Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (39 loc) · 1.35 KB
/
Copy pathserver.js
File metadata and controls
47 lines (39 loc) · 1.35 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
// This set up uses Express to serve an app in Heroku server, which costs $7/month
// Please refer to the following video for source and detailed instructions
// https://www.youtube.com/watch?v=mJrmbGvsNps
const express = require('express');
const path = require('node:path');
const os = require('os');
const app = express();
process.env.NODE_ENV = 'production';
if (process.env.NODE_ENV === 'production') {
app.use((req, res, next) => {
if (req.header('x-forwarded-proto') !== 'https')
res.redirect(`https://${req.header('host')}${req.url}`);
else next();
});
}
app.use(express.static(path.join(__dirname, '/code_source')));
app.get('*', function (request, response) {
response.sendFile(path.join(__dirname, '/code_source', '/index.html'));
});
const PORT = process.env.PORT || 8888;
// Get local IP address
const getLocalIp = () => {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const interface of interfaces[name]) {
const { address, family, internal } = interface;
if (family === 'IPv4' && !internal) {
return address;
}
}
}
return '127.0.0.1';
};
app.listen(PORT, '0.0.0.0', () => {
const localIp = getLocalIp();
console.log(`Server running at:`);
console.log(`- Local: http://localhost:${PORT}`);
console.log(`- Network: http://${localIp}:${PORT}`);
});