-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.js
83 lines (73 loc) · 2.14 KB
/
app.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
83
"use strict";
const compression = require("compression");
const express = require("express");
const app = express();
const PORT = process.env.PORT || 4000;
const UIPORT = process.env.UIPORT || 4001;
// 调试环境,加载webpack的调试中间价
if (process.env.NODE_ENV !== "production") {
console.log("Development Mode");
console.log("Using webpack-dev-middleware");
console.log("Using webpack-hot-middleware");
const bs = require("browser-sync");
const webpack = require("webpack");
const webpackDevMiddleware = require("webpack-dev-middleware");
const webpackHotMiddleware = require("webpack-hot-middleware");
const webpackConfig = require("./webpack.config.dev");
let compiler = webpack(webpackConfig);
bs.init({
port: PORT,
ui: {
port: UIPORT
},
server: {
baseDir: "public",
middleware: [
webpackDevMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
noInfo: true, // display no info to console (only warnings and errors)
quiet: false, // display nothing to the console
stats: {
colors: true
}
}),
webpackHotMiddleware(compiler)
]
},
files: [
"public/*"
]
}, function () {
console.log (`listenning on ${PORT}`);
});
/*
let compiler = webpack(webpackConfig);
app.use(webpackDevMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
noInfo: true, // display no info to console (only warnings and errors)
quiet: false, // display nothing to the console
stats: {
colors: true
}
}));
app.use(webpackHotMiddleware(compiler));
*/
} else {
console.log("Production Mode");
app.use(compression());
app.use(express.static(__dirname + "/public"));
// 404 服务
app.use((req, res, next) => {
var err = new Error("Page Not Found");
err.status = 404;
next(err);
});
// 500 服务
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.end(`message: ${err.message}`);
});
app.listen(PORT, "0.0.0.0", () => {
console.log (`listenning on ${PORT}`);
});
}