Miha Šušteršič
Miha Šušteršič

Reputation: 10052

express - Angular2 Error: ENOENT: no such file or directory upon refresh

I have a public folder in which I put an angular2 app. Now I am trying to setup an express server with a catchall route that always returns index.html. To be clear - according to this question I need to map all of my routes to index.html.

If I access the base server URL (localhost:10001), everything works as expected. But when I go to a route(let's say localhost:10001/landing), and refresh the page, I get the following error:

Error: ENOENT: no such file or directory, stat '/Users/shooshte/express-test/index.html' at Error (native)

This is my server configuration:

var express = require('express');
var static = require('serve-static');
var server = express();

// middleware
server.use(express.static(__dirname + '/public'));

// routes
server.use('*', function (req, res) {
  res.sendFile(__dirname + '/index.html');
});

var port = 10001;
server.listen(port, function() {
  console.log('server listening on port ' + port);
});

What am I doing wrong?

Upvotes: 0

Views: 1831

Answers (2)

Dinesh Agrawal
Dinesh Agrawal

Reputation: 346

i think you don't have your index.html in either public or your root folder /Users/shooshte/express-test/

this is the only resion you are getting this error

Upvotes: 0

robertklep
robertklep

Reputation: 203359

You're missing the public directory in the path to index.html:

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

Upvotes: 1

Related Questions