简体   繁体   English

Express static不会从根目录加载CSS文件

[英]Express static won't load CSS files from root

My app is loaded from a url such as : server.com/pdf/12345 . 我的应用是从以下网址加载的: server.com/pdf/12345 When it is looking for the static files, it tries to GET /pdf/12345/assets/css/stylesheet.css and 404s. 当寻找静态文件时,它尝试GET /pdf/12345/assets/css/stylesheet.cssGET /pdf/12345/assets/css/stylesheet.css I can't figure out how to tell it to look for /public/assets in the root. 我不知道如何告诉它在根目录下查找/public/assets Tried a number of different express.static configurations. 尝试了许多不同的express.static配置。

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): server.js看起来像这样(减去清晰度要求):

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 我的partials / header.ejs包含指向样式表的链接

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. 如果您以app.use('/public'开头,您是在告诉express仅将函数安装在路径/public ,就您而言,我猜该网址是否以server.com/public开头,但事实并非如此。 , express.static()永远不会触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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