【问题标题】:Path is not correct when Express sends Static html file with parametersExpress 发送带参数的静态 html 文件时路径不正确
【发布时间】:2016-07-07 00:52:14
【问题描述】:

如果这个问题在其他地方得到回答,我很抱歉。

当 url 包含参数时,我想发送静态 html 文件。 例如,http://localhost:3000/detail.html/1,但路径类型搞砸了。 这是我的问题, 我使用了 app.use(express.static(path.join(__dirname, 'public')));对于所有不带参数的静态文件请求,
对于这个http://localhost:3000/detail.html/:id 请求,我使用了

app.get('/detail.html/:id', function(req, res){
var options = {
root: __dirname + '/public/'


};

 res.sendFile(  'detail.html',options);
});

我可以在浏览器中获取detail.html页面,但是我所有的css链接和图片src in detail.html都是相对的,请求变成了这样:/detail.html/css/custom.css而不是 只是 /css/custom.css。

我的文件结构是所有html文件都在public文件夹中,css文件在public/css文件夹中。

我哪里错了? 应该如何解决这个问题? 谢谢!

【问题讨论】:

    标签: javascript express


    【解决方案1】:

    您可以将您的 detail.html 重命名为 index.html 并在您的 expressjs 代码中添加一个路由,

    app.get('/', function (req, res) {
      res.sendfile('__dirname + '/public/' + index.html);
    })
    

    然后,您的 url 中将没有 html 静态文件,一切都会正常工作

    【讨论】:

      猜你喜欢
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多