Serve static assets node js + express

link to challenge:

whats wrong with my code here :

var express = require('express');
var app = express();
absolutePath = __dirname + "views/index.html"


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



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

What is the purpose of the second argument in your express.static() function?

The API Docs of Express say this is an option argument.

Would be cool to see your live project so that we can test stuff.

changed it but still doesnt work, i pasted the url can you access that

var express = require('express');
var app = express();
absolutePath = __dirname + "views/index.html"


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



app.use(express.static("/public")



https://replit.com/@egglearn/boilerplate-express-1#myApp.js

var express = require('express');
var app = express();
absolutePath = __dirname + "views/index.html"


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



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

still not working for me ?

Sorry about my earlier suggestion. It seems the challenge has changed recently. Your code above is almost there. You will need to specify a first argument that has the relative path to the “public” files (i.e. style.css) as they would appear on the site. If you notice in the boilerplate file /views/index.html, the file styles.css is referenced as:

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

So, if you want the above file to show in browser with the above relative path, see if you can guess what relative path (generic and not file specific) you should add as the first argument to the following:

app.use(RELATIVE_TO_ROOT_PATH_GOES_HERE, express.static(__dirname + "/public"))
1 Like
var express = require('express');
var app = express();
absolutePath = __dirname + "views/index.html"


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



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

```

think this is right but doesnt work

did a few refreshes and its working, cheers Randell

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