【问题标题】:Express sendFile breaks快递发送文件中断
【发布时间】:2017-09-16 16:41:41
【问题描述】:

我不断得到:

express deprecated res.sendfile: Use res.sendFile instead 

这是我的代码:

app.get('/*', function (req, res) {

  var myUrl = req.url.split('/')[1];

  myownfunction(myUrl, function (err, rows) {

    if (rows.length != 0) {
      res.sendfile('views/article.html');
    }
    else
    {
      res.sendfile('views/404.html');
    }
  });
});

我将sendfile 更改为大写sendFile,但它中断了。 我该怎么办?

更新

当我运行我的代码时:

app.get('/upload', function(req, res){
  res.sendFile('views/upload_file.html');
});

我收到此错误: TypeError: path must be absolute or specified root to res.sendFile

【问题讨论】:

  • “它坏了”,它是怎么坏的?你遇到了什么错误?
  • @BenFortune 我用我得到的错误更新了问题:TypeError: path must be absolute or specified root to res.sendFile
  • 你试过res.sendFile(path.join(__dirname,"views/upload_file.html"))吗(你可能需要导入nodeJs的路径模块)

标签: javascript node.js express filepath sendfile


【解决方案1】:

使用path.join()函数配置文件路径。

app.get('/upload', function(req, res){
  res.sendFile(path.join(__dirname,"views/upload_file.html"))
});

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 2021-10-04
    • 2020-06-20
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    相关资源
    最近更新 更多