我有这个Express应用程序,它使用路由和hbs进行考虑。当我添加子路由(如\risi\first
)时,所有css / html文件的路径名都附加了路由名称 - risi
如何让他们访问文件?
app.js的相关代码:
var risi= require('./routes/risi');
app.use('/risi', risi);
Risi.js路线:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('risi', { title: 'RISI' }); //Works perfectly fine
});
router.get('/first', function(req, res, next) {
res.render('risi', { title: 'RISI' }); //unable to access the CSS / JS from here
});
router.get('/second', function(req, res, next) {
res.render('risi', { title: 'Joi' });
});
router.get('/third', function(req, res, next) {
res.render('risi', { title: 'Log' });
});
router.get('/fourth', function(req, res, next) {
res.render('risi', { title: 'Yo TO RISI!! <3' });
});
module.exports = router;
以下是来自控制台的错误:
GET /risi/css/main.css 404 99.4567 ms -1166
所述CSS文件位于/css/main.css
有人请帮忙!
答案 0 :(得分:2)
您应该使用绝对路径
/css/main.css
(注意前缀/
)链接到您的css文件以保证href不会附加到路径href