简体   繁体   English

Express.js静态服务无法正常工作

[英]Express.js static serving not working

I'm trying to apply a bootstrap theme to a file generated by Jade, but it says it cannot GET the file. 我正在尝试将引导主题应用于Jade生成的文件,但是它说无法获取该文件。

Main JS file: JS主文件:

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

app.set('views', __dirname + '/templates');

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

app.get('/', function(req, res){res.render('index.jade', {title: 'stuff'});});

var port = Number(process.env.PORT || 5000);
app.listen(port, function () {
  console.log('Listening on ' + port);
});

index.jade: index.jade:

link(rel='stylesheet', href='public/lumen.min.css')
h1 
   a(href='http://example.com') test
p this is a test

Any solutions on this? 有什么解决办法吗?

href='public/lumen.min.css' is trying to access a file located at __dirname + public/public/lumen.min.css (note the double public ). href='public/lumen.min.css'试图访问位于__dirname + public/public/lumen.min.css (请注意有两个public )。 Change that line to: 将该行更改为:

link(re='stylesheet', href='lumen.min.css')

You don't need to prepend the public directory to your src / href attributes, express is smart enough to figure that out. 您不需要在public目录前添加src / href属性,express足够聪明,可以弄清楚这一点。

For more examples of using express.static , check out the docs . 有关使用express.static更多示例,请查看文档

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

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