【问题标题】:express deprecated res.sendfile: Use res.sendFile insteadexpress deprecated res.sendfile: 改用 res.sendFile
【发布时间】:2014-11-08 12:32:23
【问题描述】:

在我的router/index.js 中,我使用res.sendfile(..) 如下:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res) {
    res.sendfile('public/app.html');
});

module.exports = router;

这是我的目录结构:

/Users/lucas/nodeProject/
....app.js
....public/
........app.html
....routes/
........index.js

这个例子的目的是通过http://myurl.com/而不是http://myurl.com/app.html登录我的页面。一切正常,除了我在服务器端控制台上收到以下消息:

express deprecated res.sendfile: Use res.sendFile instead

有谁知道原因,以及如何解决这个问题?只需将res.sendfile(..) 替换为res.sendFile(..) 就会出现错误:

path must be absolute or specify root to res.sendFile

我尝试了其他选项,用herehere 代替res.sendFile('app.html', { root: path.join(__dirname, '../public') });,但它只给我这个错误:

ReferenceError: path is not defined

这也是我的依赖项:

"dependencies": {
    "express": "~4.8.1",
    "static-favicon": "~1.0.0",
    "morgan": "~1.0.0",
    "cookie-parser": "~1.0.1",
    "body-parser": "~1.0.0",
    "debug": "~0.7.4",
    "jade": "*",
    "mongodb": "*",
    "mongoskin": "*",
    "connect-mongo": "*",
    "express-session": "~1.5.1"}

我对 node.js 也有点陌生,欢迎提出任何建议。

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    您也可以通过添加这些代码行来获得相同的结果:

    var dirPath = __dirname;
    app.get('/', (req, res) => {
       res.sendFile(`${dirPath}/assets/views/index.html`);
    });
    

    【讨论】:

      【解决方案2】:

      ReferenceError: path is not defined

      这意味着您需要将var path = require('path'); 放在靠近脚本顶部的某个位置。

      【讨论】:

        猜你喜欢
        • 2015-06-20
        • 2014-11-29
        • 2021-11-09
        • 2023-04-03
        • 1970-01-01
        • 2015-10-04
        • 2018-12-25
        • 2016-01-06
        • 2014-10-17
        相关资源
        最近更新 更多