-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
33 lines (24 loc) · 945 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
const express = require("express");
const morgan = require("morgan");
const path = require("path");
const { pathToFileURL, fileURLToPath } = require("url");
function serve(folderName, port) {
const configPath = pathToFileURL(path.resolve(process.cwd()));
import(`${configPath}/config.mjs`).then(({ default: config }) => {
const app = express();
app.use(morgan("tiny"));
const staticPath = path.join(fileURLToPath(configPath), folderName);
app.use(
config.base || "/",
express.static(staticPath, { redirect: false }) // Disable automatic redirection in static middleware
);
app.get("*", (req, res) => {
res.sendFile(path.join(staticPath, "index.html"));
});
const serverPort = process.env.PORT || port || config.port || 3000;
app.listen(serverPort, () => {
console.log(`Server running on port ${port} with base ${config.base}`);
});
});
}
module.exports = { serve };