3

I'm trying to render an index.html but I get the error enoent, even with the right path.

//folders tree
test/server.js
test/app/routes.js
test/public/views/index.html

//routes.js    
app.get('*', function(req, res) {
    res.sendFile('views/index.html');
});


//server.js
app.use(express.static(__dirname + '/public'));
require('./app/routes')(app);

I also tried

res.sendFile(__dirname + '/public/views/index.html');

If I use

res.sendfile('./public/views/index.html');

then it works, but I see a warning that says sendfile is deprecated and I have to use sendFile.

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
Alex
  • 1,230
  • 2
  • 24
  • 46
  • 1
    What happens when you `console.log` the path that you're putting into `sendFile`? Do you get the path you expect? – Aurora0001 Nov 30 '16 at 17:04
  • It gives me the path '/Users/me/Desktop/test/app/public/views/index.html' and it is supposed to be the correct path – Alex Nov 30 '16 at 17:05
  • Could you include the **complete** error message then? It seems odd that the path is correct yet it still doesn't work. – Aurora0001 Nov 30 '16 at 17:07
  • 1
    have you tried `res.sendFile('/views/index.html');` ? – Kevin Nov 30 '16 at 17:07
  • 1
    with res.sendFile('/views/index.html'); I get Error: ENOENT: no such file or directory, stat '/views/index.html' at Error (native) – Alex Nov 30 '16 at 17:09
  • 1
    actually if you're just serving static files the expected thing is to not have the route defined at all, thats what the static middleware does. – Kevin Nov 30 '16 at 17:10
  • the complete error is: Error: ENOENT: no such file or directory, stat '/Users/me/Desktop/test/app/views/index.html' at Error (native) – Alex Nov 30 '16 at 17:12
  • and is there a file at this path? – xShirase Nov 30 '16 at 17:14
  • Just to be sure are you trying to render the html page for viewing in a browser or are you actually trying to send a file for download ? If you just want to render you need res.render() not res.sendFile() – NiallJG Nov 30 '16 at 17:16

4 Answers4

9

Try adding :

 var path = require('path');
 var filePath = "./public/views/index.html"
 var resolvedPath = path.resolve(filePath);
 console.log(resolvedPath);
 return res.sendFile(resolvedPath);

This should clear up whether the file path is what you expect it to be

NiallJG
  • 1,881
  • 19
  • 22
  • This one worked, I see now that the path was not correct, it was seeking the folder views in the folder app instead of the folder public. Thanks to everybody for the help ;) – Alex Nov 30 '16 at 17:24
0

Try using the root option, that did it for me :

var options = {
    root: __dirname + '/public/views/',
};

res.sendFile('index.html', options, function (err) {
    if (err) {
      console.log(err);
      res.status(err.status).end();
    }
    else {
      console.log('Sent:', fileName);
    }
  });
xShirase
  • 11,975
  • 4
  • 53
  • 85
  • { Error: ENOENT: no such file or directory, stat '/Users/me/Desktop/test/app/public/views/index.html' at Error (native) errno: -2, code: 'ENOENT', syscall: 'stat', path: '/Users/me/Desktop/test/app/public/views/index.html', expose: false, statusCode: 404, status: 404 } – Alex Nov 30 '16 at 17:14
  • is there a file there? – xShirase Nov 30 '16 at 17:15
0

The problem is that you've defined static file middleware, but then you define a route in front of that that tries to handle serving the static files (so the static file middleware actually does nothing here). So if you want to res.sendFile something from a route you would need to give either an absolute path to it. Or, you could just remove the app.get('*', ...) route and let the express middleware do its job.

Kevin
  • 24,871
  • 19
  • 102
  • 158
  • `res.sendFile` always takes an absolute path, according to the API docs: "Unless the root option is set in the options object, path must be an absolute path to the file." – xShirase Nov 30 '16 at 17:13
  • OK I'll correct the answer, but my main point was that there's no need to `res.sendFile` in the first place. The route should be removed so that the middleware defined can do its job. – Kevin Nov 30 '16 at 17:15
  • yup, agreed on that – xShirase Nov 30 '16 at 17:15
0

you can try below code

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(express.static(path.join(__dirname, 'public/views')));

Handle api call

app.use('/', function(req, res, next) {
    console.log('req is -> %s', req.url);
    if (req.url == '/dashboard') {
        console.log('redirecting to  -> %s', req.url);
        res.render('dashboard');
    } else {
        res.render('index');
    }

});
mohan rathour
  • 420
  • 2
  • 12