gpt4 book ai didi

node.js - 渲染页面时不添加样式

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:58 25 4
gpt4 key购买 nike

我在渲染页面时遇到样式问题。连接任何其他页面上的样式都没有问题这是带有product.pug 的哈巴狗:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="styles/main.css">
<title>#{product.product_name}</title>
</head>
<body>
<header>
include layout/menu.pug
include layout/myProfile.pug
</header>
<main>
| there will be data about the product
</main>
include layout/footer.pug
</body>
</html>

这是 app.js 的代码:

app.get('/product/:productId', (req, res) => {
if (req.params["productId"] !== undefined &&
req.params["productId"] > 0) {
conn.query(`SELECT *
FROM product
WHERE product_id = ${req.params["productId"]}`, (err, product) => {
if(err) {throw err;}
if(product.length > 0) {
res.render('product', {
userName: req.session.userName,
successAuthentication: req.session.successAuthentication,
isWorker: req.session.isWorker,
product
})
} else {
res.sendStatus(404);
}
});
} else {
res.sendStatus(404);
}
});

样式存放在public文件夹中,app用于使用样式

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

任何其他页面都没有样式问题。我无法解决这个问题,所以我将不胜感激任何提示

最佳答案

您可以像下面的代码一样设置快速静态文件夹:

app.use(expresss.static('public'));

现在,您可以尝试在应用根目录中创建 public 文件夹,并将 styles 文件夹移动到 public 文件夹中,它会正常工作。

希望对你有帮助。

关于node.js - 渲染页面时不添加样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59956595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com