简体   繁体   中英

Express static won't load CSS files from root

My app is loaded from a url such as : server.com/pdf/12345 . When it is looking for the static files, it tries to GET /pdf/12345/assets/css/stylesheet.css and 404s. I can't figure out how to tell it to look for /public/assets in the root. Tried a number of different express.static configurations.

my directory structure looks like this:

public
--assets
----css
views
--partials
----header.ejs
routes
--api.js
server.js

server.js looks like this (minus the requires for clarity):

app.use('/', routes);
app.set('views', './views');
app.set('view engine', 'ejs');

app.use('/public', express.static(path.join(__dirname, 'public')));
http.createServer(app).listen(port, function() {
})

My partials/header.ejs contains link to stylesheet

You should write it like this instead:

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

If you start with app.use('/public' you are telling express to only mount the function on the path /public , in your case i guess if the url starts with server.com/public . But since that is not the case, express.static() never fires.

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