最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - express deprecated res.sendfile: Use res.sendFile instead - Stack Overflow

programmeradmin3浏览0评论

In my router/index.js, I am using res.sendfile(..) as follows:

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

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

module.exports = router;

and here is my directory structure:

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

The purpose of this example is to log into my page via / instead of .html. Everything works fine, except that I get the following message on my server-side console:

express deprecated res.sendfile: Use res.sendFile instead

Does anyone know the cause, and how to fix this? Simply substituting res.sendfile(..) for res.sendFile(..) gives me the error:

path must be absolute or specify root to res.sendFile

I have tried other options, outlined here and here to substitute res.sendFile('app.html', { root: path.join(__dirname, '../public') });, but it only gives me this error:

ReferenceError: path is not defined

Here are my dependencies as well:

"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"}

I am also a bit new to node.js, so any suggestions are wele.

In my router/index.js, I am using res.sendfile(..) as follows:

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

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

module.exports = router;

and here is my directory structure:

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

The purpose of this example is to log into my page via http://myurl./ instead of http://myurl./app.html. Everything works fine, except that I get the following message on my server-side console:

express deprecated res.sendfile: Use res.sendFile instead

Does anyone know the cause, and how to fix this? Simply substituting res.sendfile(..) for res.sendFile(..) gives me the error:

path must be absolute or specify root to res.sendFile

I have tried other options, outlined here and here to substitute res.sendFile('app.html', { root: path.join(__dirname, '../public') });, but it only gives me this error:

ReferenceError: path is not defined

Here are my dependencies as well:

"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"}

I am also a bit new to node.js, so any suggestions are wele.

Share Improve this question edited May 23, 2017 at 12:03 CommunityBot 11 silver badge asked Sep 15, 2014 at 3:05 modulitosmodulitos 15.9k17 gold badges82 silver badges120 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

ReferenceError: path is not defined

This means you need to put var path = require('path'); somewhere near the top of your script.

you may also be able to get the same result by adding these lines of code:

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

评论列表(0)

  1. 暂无评论