-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (40 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const colors = require('colors'),
cors = require('@koa/cors'),
Koa = require('koa'),
path = require('path');
const common = require('./common/common'),
setup = require('./config/setup');
let base = path.normalize(__dirname);
colors.setTheme({
info: 'blue',
error: 'red'
});
let app = new Koa();
app.use(cors({origin: '*'}));
module.exports = app;
/**
* Initiates a new server
*/
app.init = async function() {
console.log('Setting up your StaticMockServer!'.info.underline);
//console.dir(process.argv);
let port = process.env.PORT || 3001;
if (process.argv.length > 2) {
let json = await common.asyncRead(path.join(base, process.argv[2]));
// koa config
setup(app, json);
} else {
console.warn(`Starting up the StaticMockServer with nothing loaded, please make a GET request to <URL>:${port}/load/ with the filename you want to load from the mocks directory.`.magenta);
setup(app);
}
// create http and start listening for requests
app.server = app.listen(port);
console.log(`StaticMockServer listening on port ${port}`.green);
};
// auto init if this app is not being initialized by another module (i.e. using require('./app').init();)
if (!module.parent) {
app.init().catch(function (err) {
console.error(err);
process.exit(1);
});
}