2

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.com/ instead of http://myurl.com/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 welcome.

Community
  • 1
  • 1
modulitos
  • 14,737
  • 16
  • 67
  • 110

2 Answers2

5

ReferenceError: path is not defined

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

mscdex
  • 104,356
  • 15
  • 192
  • 153
1

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`);
});
CodeJammer
  • 31
  • 1
  • 9