简体   繁体   中英

Express js does not serve static files

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 eg

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 ?

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')))

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

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

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM