-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
31 lines (25 loc) · 926 Bytes
/
server.js
File metadata and controls
31 lines (25 loc) · 926 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
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const root = path.join(__dirname, 'public');
app.use((req, res, next) => {
let filePath = path.join(root, decodeURIComponent(req.path));
if (
(fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()) ||
(fs.existsSync(filePath + "index.html") && fs.lstatSync(filePath + "index.html").isFile())
) {
console.log("🛜 serving " + filePath.replace(root, ""));
return res.sendFile(filePath);
} else if (fs.existsSync(filePath + '.html')) {
console.log("🛜 serving " + filePath.replace(root, ""));
return res.sendFile(filePath + '.html');
} else {
console.log("⚠️ 404 could not find " + filePath.replace(root, ""));
}
next();
});
app.use(express.static(root));
app.listen(8000, () => {
console.log('🚀 Local server running at http://localhost:8000');
});