【问题标题】:Node js routing confusionNode js 路由混乱
【发布时间】:2020-04-26 17:31:02
【问题描述】:

有没有更好的处理方法?我只是想用css和客户端js渲染一个html页面。

app.get('/:path', function(req, res) {
    let path = req.params.path

    if (path.endsWith(".html")) {
        res.render("./" + path)
        return
    }

    fs.readFile("./" + path, null, (err, data) => {
        if (err) {
            console.log(err)
        } else {
            res.send(data)
        }
    })
});

【问题讨论】:

    标签: javascript html node.js routes render


    【解决方案1】:

    Express js 提供 express.static 函数

    app.use(express.static('foldername'))

    在文件夹内你可以创建 html,css js 文件,所以你的 url 将是 localhost/filename.html filename.html 直接调用你新建的文件夹文件。

    供您参考: https://expressjs.com/en/starter/static-files.html

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      相关资源
      最近更新 更多