Problem in Serve Static Assets code in Replit

My code is not rendering css file from public
I code app.use() method but not rendering asset files from the /public directory to the /public path

Your code so far
//Serve Static Assets
app.use(express.static(__dirname + “/public”));

link :: https://replit.com/@SumitSapate/boilerplate-express-2#myApp.js

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0

Challenge: Serve Static Assets

Link to the challenge:

If you tell express to use a file from the public folder like you do here:

app.use(express.static(__dirname + '/public'))

then you can reference to it simply like this (in your index.html):

<link rel="stylesheet" href="style.css">

Conversely, if you want to reference files like so:

<link rel="stylesheet" href="/public/style.css">

you need to specify a mount path for the static directory:

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

See serving static files in Express.

Cheers

Thanks for support but its not working on my replit !
tired both the ways

app.use(express.static(__dirname + '/public'))
<link rel="stylesheet" href="style.css">

:point_up_2:works but not passing the test case

app.use(express.static('/public',  __dirname + '/public'))
<link rel="stylesheet" href="/public/style.css">

:point_up_2: Not working at all

I’m sorry, I didn’t mean to confuse you.
The correct syntax is:

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

If you look at the test result you should see

Screen Shot 2022-03-13 at 18.09.51

which, I believe, is telling you to specify a mount path for the static directory.

I hope this clarifies.

Cheers

1 Like

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