To serve static files on a dynamic route using Node Express, we call the sendFile
method.
For instance, we write
app.get("/user/:uid/files/*", (req, res) => {
const uid = req.params.uid;
const path = req.params[0] ? req.params[0] : "index.html";
res.sendFile(path, { root: "./public" });
});
to call app.get
to add a get route that gets the URL parameters from req.params
.
And we use that to create the path
for the file.
Then we call res.sendFile
to send the file at the path
within the root
folder as the response.