-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
49 lines (44 loc) · 1.67 KB
/
index.mjs
File metadata and controls
49 lines (44 loc) · 1.67 KB
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
import express from 'express';
import cookieParser from 'cookie-parser';
import methodOverride from 'method-override';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import webpackConfig from './webpack_conf/webpack.dev.js';
import bindRoutes from './routes.mjs';
// Initialise Express instance
const app = express();
// Set the Express view engine to expect EJS templates
app.set('view engine', 'ejs');
// Bind cookie parser middleware to parse cookies in requests
app.use(cookieParser());
// Bind Express middleware to parse request bodies for POST requests
app.use(express.urlencoded({ extended: false }));
// Bind Express middleware to parse JSON request bodies
app.use(express.json());
// Bind method override middleware to parse PUT and DELETE requests sent as POST requests
app.use(methodOverride('_method'));
// Expose the files stored in the public folder
app.use(express.static('public'));
// Expose the files stored in the distribution folder
app.use(express.static('dist'));
// Set up Webpack in dev env
const env = process.env.NODE_ENV || 'development';
if (env === 'development') {
const compiler = webpack(webpackConfig);
app.use(webpackDevMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
// html only
writeToDisk: (filePath) => /\.html$/.test(filePath),
}));
app.use(webpackHotMiddleware(compiler, {
log: false,
path: '/__webpack_hmr',
heartbeat: 5 * 1000,
}));
}
// Bind route definitions to the Express application
bindRoutes(app);
// Set Express to listen on the given port
const PORT = process.env.PORT || 3004;
app.listen(PORT);