-
-
Notifications
You must be signed in to change notification settings - Fork 488
/
startServer.js
55 lines (45 loc) · 1.28 KB
/
startServer.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
const path = require('path');
const express = require('express');
const opener = require('opener');
const { bold } = require('chalk');
const projectRoot = path.resolve(__dirname, '..', '..');
module.exports = startServer;
function startServer(chartData, opts) {
if (!chartData) {
throw new Error('chartData was not set! It should be present at this point');
}
if (!opts) {
throw new Error('Options parameter is missing');
}
if (!opts.logger) {
throw new Error('A logger is missing from the options parameter');
}
const {
port = 8888,
openBrowser = true,
logger
} = opts;
const app = express();
// Explicitly using our `ejs` dependency to render templates
// Fixes #17
app.engine('ejs', require('ejs').renderFile);
app.set('view engine', 'ejs');
app.set('views', `${projectRoot}/views`);
app.use(express.static(`${projectRoot}/public`));
app.use('/', (req, res) => {
res.render('viewer', {
mode: 'server',
chartData: JSON.stringify(chartData)
});
});
return app.listen(port, () => {
const url = `http://localhost:${port}`;
logger.info(
`${bold('Webpack Bundle Analyzer')} is started at ${bold(url)}\n` +
`Use ${bold('Ctrl+C')} to close it`
);
if (openBrowser) {
opener(url);
}
});
}