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

标签 node.js express pug

我在渲染页面时遇到样式问题。 连接任何其他页面上的样式都没有问题 这是带有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/

相关文章:

node.js - 表示 res.redirect() 不执行任何操作

javascript - 如何在javascript下划线模板中转义<>?

javascript - 继承javascript Number 改为String

javascript - 在 WebStorm 中向异步 Node.js mocha 测试添加代码覆盖率

node.js - 使用 ES6 和默认创建的模型关联(模型与模型无关)

node.js - 什么是 nodejs express 管理多个域?

node.js - 如何在 Jade 中包含一个css文件(不链接它)

node.js - 从mongodb(mongoose)获取数据到jade view

node.js - 使用pm2启动node.js应用程序: bad gateway from proxy_verse server

node.js - OAuth2 回调 - 识别用户?