2

I am beginner with node. I am trying to serve a file. The hierarchy of my project looks like

app

  modules
  node_modules
  public
    css
      index.css
    html
      index.html
    javascript
  routes
     main.js

inside main.js

var express = require('express');
var app     = express();
var path    = require('path')
var port    = 8080;

app.use("/styles",  express.static(path.join(__dirname + '../public/css')));
app.use("/scripts", express.static(__dirname + '../public/javascript'));

app.get('/' , function( req , res ){
    res.sendFile(path.join(__dirname,'../public/html/index.html'))
})
app.listen(port)

i want to serve a file on / route. It works fine , but css and javascripts are not loaded - it throws error in browswer console

http://localhost:8080/css/index.css Failed to load resource: the server responded with a status of 404 (Not Found)

What is the right way to set path to css? I have trouble to find the righ solution.

Thanks!

2
  • 1
    your static css route is called "styles", not "css"; try http://localhost:8080/styles/index.css Commented Aug 29, 2016 at 19:52
  • @Hamms it doesnt find the file either , why styles? Commented Aug 29, 2016 at 19:55

2 Answers 2

3

The problem is with your path definition. When you use path.join you should pass strings to it and this method will join them with platform specific separator as a delimiter, then normalizes the resulting path.

so your styles path should be:

app.use("/styles",  express.static(path.join(__dirname, 'public', 'css')));

and your styles will serve from:

http://localhost:8080/styles/index.css

Because you are using a virtual path prefix (here: /styles).

If you dont mind platform specific separator, i. e. know that your server will be a unix like environment and dont need a virtual path prefix then just use:

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

and your styles will serve from css dir:

http://localhost:8080/css/index.css
Sign up to request clarification or add additional context in comments.

8 Comments

The final style route won't be served from localhost:8080/index.css but rather localhost:8080/css/index.css
@Josh, make sure that you're not using relative paths within your HTML file when loading your css. ../css/index.css won't be a valid route however, /css/index.css is a valid route.
@peteb, i checked just now and it serves from root: http://localhost:8080/index.css
@dNitro yes, that's true if you're going to make the static route /public/css instead of /public.
pastebin.com/4byK7Ukk this is my current code , hierarchy stays the same , i run it by node main.js , i rly do not know what would work
|
1

Because you set another routes. Change your routes to static files. Use this:

app.use(express.static('../public'));

2 Comments

The relative path for ../public is unnecessary when using __dirname because that's precisely what __dirname provides. app.use(express.static(path.join(__dirname, 'public'); is the correct way to use path.join(). The + is counter to the reason why you'd use path.join().
@peteb you are right, it's my mistake. I've fixed it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.