-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.dev.js
83 lines (71 loc) · 1.93 KB
/
server.dev.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @file server.dev.js
* @author lavas
*/
var LavasCore = require('lavas-core-vue');
var express = require('express');
var stoppable = require('stoppable');
var proxy = require('http-proxy-middleware');
var configPath = './lavas.config.js';
// fix https://github.com/lavas-project/lavas/issues/50
if (process.argv.length >= 3 && process.argv[2] !== 'dev') {
configPath = process.argv[2];
}
/**
* API Proxy Configuration
*
* @see https://github.com/chimurai/http-proxy-middleware
* @type {Object}
*/
var proxyTable = {
// proxy table example
// '/api': {
// target: 'https://lavas.baidu.com',
// changeOrigin: true
// }
};
var port = process.env.PORT || 3000;
var core = new LavasCore(__dirname);
var app;
var server;
/**
* start dev server
*/
function startDevServer() {
app = express();
core.build()
.then(function () {
// API Proxying during development
Object.keys(proxyTable).forEach(function (pattern) {
app.use(pattern, proxy(proxyTable[pattern]));
});
app.use(core.expressMiddleware());
/**
* server.close() only stop accepting new connections,
* we need to close existing connections with help of stoppable
*/
server = stoppable(app.listen(port, function () {
console.log('server started at localhost:' + port);
}));
})
.catch(function (err) {
console.log(err);
});
}
/**
* every time lavas rebuild, stop current server first and restart
*/
core.on('rebuild', function () {
core.close().then(function () {
server.stop();
startDevServer();
});
});
core.init(process.env.NODE_ENV || 'development', true, {configPath})
.then(function () {
startDevServer();
});
// catch promise error
process.on('unhandledRejection', function (err) {
console.warn(err);
});