Basic Node and Express - Serve Static Assets

After reading the docs and the FCC explanation, I’m still stuck on the concept of middleware. For example

// accesses __dirname + /public
app.use(express.static(__dirname + "/public"));

// accesses /public + __dirname + /public (?)
app.use("/public", express.static(__dirname + "/public"));

If my comments are correct, why can’t I just write

app.use(express.static("/public" + __dirname + "/public"));

Maybe this will help answer it.

Thank you for your response.

Just to check if I’m understanding correctly, this:

app.use("/public", express.static(__dirname + "/public"));

Could look like this:“/public”)(__dirname + “/public”)

For example:

app.use("/styles", express.static(__dirname + "/styles"));
// let's say the only file in styles is a css file called cool.css


Would this be correct?

Yes, that would be correct.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.