-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
51 lines (44 loc) · 1.77 KB
/
index.mjs
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
import cookieParser from 'cookie-parser';
import express from 'express';
import methodOverride from 'method-override';
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 { default: webpack } = await import('webpack')
const { default: webpackDevMiddleware } = await import('webpack-dev-middleware');
const { default: webpackHotMiddleware } = await import('webpack-hot-middleware');
const { default: webpackConfig } = await import('./webpack_conf/webpack.dev.js');
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: 10 * 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);