Skip to content

Commit

Permalink
Merge pull request #393 from JulianKniephoff/simplify-static-server
Browse files Browse the repository at this point in the history
Further simplify the static file server
  • Loading branch information
Arnei authored May 16, 2024
2 parents 6f4382d + a7f6d07 commit ae88eeb
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions staticServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ const path = require("path");
const express = require("express");

const app = express();
const port = process.env.PORT || 5000;

app.get("/*", express.static(path.join(__dirname, "test/app/GET")));
for (const method of ["post", "put", "delete"]) {
app[method]("/*", (req, res, next) => {
setTimeout(next, 1000);
});
}

app.post("/*", (req, res, next) => {
res.status(201);
next();
});

const serveStatic = (req, res) => {
const filePath = path.join(__dirname, "test/app", req.method.toUpperCase(), req.url);
setTimeout(() => {
res.sendFile(filePath);
}, 1000);
};

for (const method of ["put", "post", "delete"]) {
app[method]("/*", serveStatic);
}
app.use("/", [
(req, res, next) => {
req.url = `/${req.method}${req.url}`;
req.method = "GET";
next();
},
express.static(path.join(__dirname, "test/app"))
]);

const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Listing on port ${port}`));

0 comments on commit ae88eeb

Please sign in to comment.