-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
55 lines (43 loc) · 1.29 KB
/
server.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
require('babel-register')
const webpack = require('webpack');
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const config = require('./webpack.config');
const isProduction = process.env.NODE_ENV === 'production';
const isDeveloping = !isProduction;
const app = express();
// Webpack developer
if (isDeveloping) {
const compiler = webpack(config);
app.use(require('webpack-dev-middleware')(compiler, {
publicPath: config.output.publicPath,
noInfo: true
}));
app.use(require('webpack-hot-middleware')(compiler));
}
const publicPath = path.resolve(__dirname);
app.use(bodyParser.json({ type: 'application/json' }))
app.use(express.static(publicPath));
const port = isProduction ? (process.env.PORT || 80) : 3033;
require('./router.config')(app);
// 500
app.use(function respondError(err, req, res, next) {
console.log('500');
var status, errmsg;
status = err.status || 500;
res.status(status);
errmsg = err.message || 'oo there was a problem!';
if(req.method === 'GET'){
res.status('500').send('服务器错误:'+errmsg)
}else{
res.type('txt').send(errmsg + '\n');
}
});
// Listen
app.listen(port, function (err, result) {
if(err){
console.log(err);
}
console.log('Server running on 127.0.0.1:%s', port);
});