-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
37 lines (30 loc) · 845 Bytes
/
server.js
File metadata and controls
37 lines (30 loc) · 845 Bytes
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
const express = require('express');
const fs = require('fs');
const path = require('path');
const PORT = 3000;
const app = new express();
const publicPath = path.resolve(__dirname, './public');
app.use(function handler(req, res) {
const fileName = path.join(publicPath, req.url);
fs.stat(fileName, (err, stats) => {
if (!err && stats.isFile()) {
log('REQ: ' + req.url + '. Send file ' + fileName);
res.sendFile(fileName);
}
else {
log('REQ: ' + req.url + '. Send index.html');
res.sendFile(path.join(publicPath, './index.html'));
}
});
});
app.listen(PORT, function handler(error) {
if (error) {
console.error(error);
}
else {
console.info('[SERVER] Started at port %s', PORT, PORT);
}
});
function log(msg) {
return console.log('[' + new Date().toString() + '] ' + msg);
}