1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const path = require('path');
|
7 | const express = require('express');
|
8 | const webpack = require('webpack');
|
9 | let config = require('../webpack.config');
|
10 |
|
11 | module.exports = (cherryConfig) => {
|
12 |
|
13 |
|
14 |
|
15 | const sc = cherryConfig.server;
|
16 |
|
17 | const app = express();
|
18 |
|
19 | const compiler = webpack(config);
|
20 |
|
21 | app.use(require('webpack-dev-middleware')(compiler, {
|
22 | noInfo: true,
|
23 | publicPath: config.output.publicPath
|
24 | }));
|
25 |
|
26 | app.use(require('webpack-hot-middleware')(compiler, {
|
27 | reload: false
|
28 | }));
|
29 |
|
30 | app.use(express.static(path.resolve(__dirname, '../site') ) );
|
31 |
|
32 |
|
33 | if (sc.middleware && sc.middleware.length) {
|
34 | sc.middleware.forEach(item => {
|
35 | app.use(item);
|
36 | });
|
37 | }
|
38 |
|
39 | app.get('*', function (req, res) {
|
40 |
|
41 | res.send('404!!!');
|
42 | });
|
43 |
|
44 |
|
45 | const port = sc.port || 9000;
|
46 | app.listen(port, function (err) {
|
47 | if (err) {
|
48 | console.log(err);
|
49 | return;
|
50 | }
|
51 | console.log(`Listening at http://localhost:${port}`);
|
52 | });
|
53 | }
|