4

I have mounted path like this

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

admin.use(express.static(path.join(__dirname, 'public/')));

// mount admin path
app.use('/admin', admin);

Then I defined some route e.g

admin.get('/jobs', function(req, res){ 
   res.render('jobs/index');
});

admin.get('/jobs/create', function(req, res){ 
   res.render('jobs/create');
});

In the first route static files such as js,css,images loaded w/o problems. But in second one it does not load files.

Files loaded in views like this

<link rel="stylesheet" href="styles/css/main.css">

NOTE : styles directory is under public folder in my working directory.

So what is the problem ? What I did wrong ?

1
  • Use <link rel="stylesheet" href="/styles/css/main.css">. You need to add / at the starting for static assets. Commented Jul 22, 2016 at 15:56

2 Answers 2

3

The static middleware is added to the admin express instance, which is mounted in the main app at the /admin route. This means it will never get called for routes other than those matching /admin. Move your middleware to the main app instance instead,

app.use(express.static(path.join(__dirname, 'public')))
Sign up to request clarification or add additional context in comments.

3 Comments

That is the problem . I want to serve different static files for admin and for front. That's why I added different static folders . What is really interesting it works with /admin/jobs but does not for /admin/jobs/create.
The reason it works for /admin/jobs, but not /admin/jobs/create is because you are using a relative path to your stylesheet. So for /admin/jobs it resolves to /admin/styles/css/main.css (which works), while for /admin/jobs/create it resolves to /admin/jobs/styles/css/main.css (which doesn't). You can instead use the absolute path /admin/styles/css/main.css, i.e. <link rel="stylesheet" href="/admin/styles/css/main.css">, which should work.
Alternatively, if you always want to serve your stylesheet from the public url /styles/css/main.css, then simply create a separate route for it, along the lines of app.get('/styles/css/main.css', (req, res) => res.sendFile(...)). You can include logic in this route handler to determine what stylesheet to serve the user.
0

First: app.use(express.static(path.join(__dirname, 'public')));

Then href="/styles/css/main.css"?

Hope it helps.

3 Comments

No . I modified code . added slash after public please have a look . Still does not work .
Well, public directory is accesbile for static files at / path. So the slash you added at the end does nothing. You have to access to absolute path /public from the views.
Edited answer does not work even for the first route .

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.