-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
43 lines (37 loc) · 998 Bytes
/
server.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
const express = require('express');
const opn = require('opn');
const ip = require('ip');
const app = express();
const options = process.argv.reduce((acc, arg) => {
const pair = arg.split('=');
if (pair.length === 2) {
acc[pair[0]] = pair[1];
}
return acc;
}, {});
app.get('/ip', (req, res) => {
res.send(ip.address());
});
app.get(/\/myApp(.*)(\/|\.html)$/, (req, res, next) => {
if (req.query.key || !options.key) {
next();
} else {
if (req.originalUrl.slice(-10) === 'index.html') {
res.redirect(`${req.originalUrl}?key=${options.key}`)
} else {
res.redirect(`./index.html?key=${options.key}`)
}
}
});
app.use(express.static('public'));
app.use('/public', express.static('public'));
app.use('/src', express.static('src'));
app.use('/dist', express.static('dist'));
app.listen(3000, '0.0.0.0', (err) => {
if (err) {
console.error(err);
} else {
console.info(`Listening at localhost:3000 (http://${ip.address()}:3000)`);
opn(`http://localhost:3000/`);
}
});