Can't pass the "Serve Static Assets" lesson, please help!

I do not know what I am doing wrong with this exercise, I’m ussing https://repl.it/ and I pasted the same line of code as in the solution

Hope someone can help me! Thanks in advance

And this is my code:

var express = require('express');
var app = express();

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

app.get("/", (req, res)=>{
  res.sendFile( __dirname + "/views/index.html");
})

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36.

Challenge: Serve Static Assets

Link to the challenge:

Remember that static assets should be served on the /public path.

I think the wording of this challenge is confusing (or maybe the setup is just wrong). It tells you to use the path __dirname + '/public', but the CSS file linked in the template HTML already has the /public part in its href: /public/style.css. Which I think is wrong, because the href is essentially /public/public/style.css now.

Solution is to only use __dirname in the .static method.

4 Likes

@sanity but isn’t it being served on the /public in the code that I attached?

Currently it’s serving on / path. From /public directory, but not on /public path.

1 Like

I’m confused, there’s a route / which serves a file from the views folder (because that’s where the index.html is located), and any static asset files are served from the public folder:

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

app.get("/", (req, res)=>{
  res.sendFile( __dirname + "/views/index.html");
})

Have you looked at the challenge?

Basically, middleware are functions that intercept route handlers, adding some kind of information. A middleware needs to be mounted using the method app.use(path, middlewareFunction) . The first path argument is optional. If you don’t pass it, the middleware will be executed for all requests.

Mount the express.static() middleware to the path /public with app.use() . The absolute path to the assets folder is __dirname + /public .

Well yes. But mounting this:

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

won’t pass the challenge.

Um, that’s what I have been saying…

Nevermind :+1:
20chars

[quote=“jsdisco, post:3, topic:448158”]
href: /public/style.css
[/quo The only way to make that pass the challenge, is if you change in the html the reference to the css file, just by deleating the /public part.

1 Like

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