-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (67 loc) · 2.36 KB
/
Copy pathindex.js
File metadata and controls
76 lines (67 loc) · 2.36 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
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
const fs = require('fs');
const path = require('path');
const express = require('express');
const webpack = require('webpack');
const devMiddleware = require('webpack-dev-middleware');
const hotMiddleware = require('webpack-hot-middleware');
const webpackConfigBuilder = require('./webpack.config.js');
const { updateContent } = require('./middleware.js');
const ENV = 'development';
const hostname = process.argv[3] || '127.0.0.1';
const port = 3333;
const outputPath = path.join(__dirname, 'out');
const webpackConfig = webpackConfigBuilder(ENV, outputPath);
const compiler = webpack(webpackConfig);
const devMiddlewareOptions = {
// writeToDisk: true,
};
const app = express();
app.use(devMiddleware(compiler, devMiddlewareOptions));
app.use(hotMiddleware(compiler));
// Middleware to inject script tag into HTML files
app.use(async (req, res, next) => {
let filePath = path.join(outputPath, req.path === '/' ? 'index.html' : req.path);
if (path.extname(filePath) === '' && fs.existsSync(`${filePath}.html`)) {
filePath += '.html';
}
if (path.extname(filePath) !== '.html' && fs.existsSync(`${filePath}.html`)) {
filePath += '.html';
}
if (!path.basename(filePath).startsWith("_") && path.extname(filePath) === '.html' && fs.existsSync(filePath)) {
try {
const html = await fs.promises.readFile(filePath, 'utf8');
res.setHeader('Content-Type', 'text/html');
res.send(updateContent(html, {
relativePath: path.relative(outputPath, filePath),
genSearchWidgetConfigId: process.env.GEN_SEARCH_WIDGET_ID || '',
gtmId: process.env.GTM_ID || '',
environment: ENV
}));
} catch (err) {
next();
}
} else {
next();
}
});
// Serve static files from /out
app.use(express.static(outputPath));
// 404 error handler - must be placed after all other routes
app.use((req, res, next) => {
const notFoundPath = path.join(__dirname, 'public', '404.html');
if (fs.existsSync(notFoundPath)) {
try {
const html = fs.readFileSync(notFoundPath, 'utf8');
res.status(404);
res.setHeader('Content-Type', 'text/html');
res.send(html);
} catch (err) {
res.status(404).send('404 - Page Not Found');
}
} else {
res.status(404).send('404 - Page Not Found');
}
});
app.listen(port, hostname, () => {
process.stdout.write(`Server running at http://${hostname}:${port}/\n`);
});