Marc Delerue
Marc Delerue

Reputation: 65

NodeJS and express, ENOENT on view file

I've got a weird trouble with my nodeJS app since I refactored it.

The app starts well, API answers correctly but when I try to go to /, I get this error : Error: ENOENT, stat '/views/index.html' in my browser.

I now use this folder tree :

And here is the content of my server.js file :

var express = require('express');
var app = express();
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var server = require('http').Server(app);
var io = require('socket.io')(server);
var fs = require('fs');
var nconf = require('nconf');

app.use(express.static(__dirname + '/../front'));
app.use(express.static(__dirname + '/../node_modules'));
app.use(express.static(__dirname + '/../bower_components'));

app.use(morgan('dev'));

app.use(bodyParser.urlencoded({'extended': 'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({type: 'application/vnd.api+json'}));
app.use(methodOverride());

server.listen(8081);

(...) // some code to define API routes

app.get('/', function (req, res) {
    res.sendfile('/views/index.html');
});

I tried to comment app.use(express.static(__dirname + '/../front')); and to call the view using '/front/views/index.html' but the result is the same.

Upvotes: 0

Views: 584

Answers (3)

Marc Delerue
Marc Delerue

Reputation: 65

As @Sean3z suggested, I tried to change my sendfile declaration and got another error (Forbidden, sendStream error).

Finally I managed to make it work by changing my static files definition : app.use('/front', express.static(__dirname + '/../front'));

And by modifying the sendFile : res.sendfile('front/views/index.html');

Strangely, nodeJS understands (but not me :) ) and call the right file at the right place. I just need to correct my calls in the differents file to be ok.

Thanks for the answers.

Upvotes: 0

Sean3z
Sean3z

Reputation: 3735

ENOENT means Error NO ENTry which ultimately means it's not able to find your file.

int ENOENT

No such file or directory. This is a "file doesn't exist" error for ordinary files that are referenced in contexts where they are expected to already exist.

Your server is attempting to send a file from the root directory of your machine (/views/index.html). You'll probably need to adjust this to fit your file structure.

app.get('/', function (req, res) {
    res.sendfile(__dirname + '/../font/views/index.html');
});

Upvotes: 3

Nesh
Nesh

Reputation: 2541

I believe you missed to set your views folder.

app.set('views', 'MY_DIR_PATH');

Upvotes: 1

Related Questions