Express static method not working - stuck

Tell us what’s happening:
Hi everybody, I’m stuck and I can’t understand why.

I am trying to use the “express.static()” method as the middleware to serve static assets from the “public” folder, as explained in the help section. But I get the " Your app should serve asset files from the /public directory" and can’t pass the challenge.

Could someone please help.

Your code so far
app.use("/assets", express.static(__dirname + “/public”));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36.

Challenge: Serve Static Assets

Link to the challenge:

Hello! Welcome to the community :partying_face:!

Could you share your repl.it code :slight_smile:? The test requires you to serve a file called style.css which you may not have or the code may be failing to serve it.

1 Like

Hi, thanks for your reply.
Of course here is my ripl.it code : https://repl.it/join/kfoycreu-matdomichard

1 Like

You should remove the '/assets' part for it to work:

app.use(express.static(...))

Hope it helps :slight_smile:.

Thank you so much @skaparate. I spent so much time on it but I didn’t think of that !
Have a great day

1 Like

A longer explanation…

If you pass the first parameter to app.use, you’re telling express to do whatever the second parameter does to that path (the express.static in this case).

In this case, you are telling it to serve everyting under public as /assets, hence the HTML files should have paths pointing to the /assets route instead, like this:

<link href="/assets/style.css" />

Without the /assets, the route is the root:

<link href="/style.css" />
1 Like

Thanks for the explanation.

1 Like

For others having trouble with errors: you may need to just click the Stop and Start 4-5 times and keep resubmitting your code. In other words, you can have the right answer and it still throws errors insisting the path is “/public” even though you have that. Eventually it catches on and passes.

1 Like