问题描述
我正在尝试为 pug 和 express 项目中的 style.css 收费。 我尝试了许多在其他主题中发现的技巧,但都注意到了效果。
这里是 app.js 文件:
const app = express();
const pug = require('pug');
const path = require('path')
const helmet = require('helmet')
app.set('view engine','pug');
app.use(helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],scriptSrc: ["'self'"],styleSrc: ["'self'"]
}
}));
app.use(express.static(path.join(__dirname + '/views')));
console.log(app.use(express.static(path.join(__dirname + '/views'))))
app.get('/',(req,res,next) => {
res.render('index');
})
app.listen(8080);
这里是 index.pug 文件:
head
Meta(http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval' http://localhost:8080")
link(rel="stylesheet" href="/views/css/style.css")
body
h1='hello world'
我已经尝试删除链接标签的 rel 属性和其他基本内容。 谢谢。
解决方法
这对我来说是这样的:
router.use('/',express.static(path.join(__dirname,'views')));
获取资源:
link(rel="stylesheet" href="/css/style.css")
如果您想要或需要在您的网址中使用 views
这个词,那么这种组合应该可以:
router.use('/views','views')));
link(rel="stylesheet" href="/views/css/style.css")