-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
34 lines (30 loc) · 1.25 KB
/
app.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
const express = require('express')
const app = express()
const path = require('path');
let relativeFilePath = process.argv[2];
let port = process.argv[3] || 3000;
if (parseInt(relativeFilePath) > 0 && !process.argv[3]) {
port = relativeFilePath;
relativeFilePath = null;
}
if (!relativeFilePath) {
console.log('No relative filepath given. Did you forget to add it as an argument?');
console.log('Expected:');
console.log('$ node app.js ../path/to/index.html [port]');
console.log('or if using pm2');
console.log('$ pm2 start app.js -- ../path/to/index.html [port]');
console.log('');
console.log('Serving JSON instead');
console.log('');
app.get('/', (req, res) => res.json({ "status": "ok" }));
// process.exit(1);
} else {
const staticIndex = path.join(__dirname, relativeFilePath);
console.log("Serving index from:", staticIndex);
const relativeDirectory = relativeFilePath.replace('/index.html', '');
const staticDirectory = path.join(__dirname, relativeDirectory);
console.log("Serving assets from:", staticDirectory);
app.use(express.static(staticDirectory))
app.get('/', (req, res) => res.sendFile(staticIndex));
}
app.listen(port, () => console.log(`Example app listening on port ${port}!`))