Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node, Angular Error: ENOENT: no such file or directory

Tags:

I have been following the a scotch.io tutorial to create my first node and angular app. I have seen that relative paths are a common issue online for the Error: ENOENT: no such file or directory which as far as I can tell is the same as the tutorial so I'm why its not working.

The full error message is: Error: ENOENT: no such file or directory, stat'/Users/badman/githubRepos/travelGuide/app/public/index.html' at Error (native)

My folder structure is here. My server.js:

// set up web server
var express = require('express');
var app = express();
var bodyParser = require("body-parser");

// routes
require('./app/routes.js')(app);

// listen (start app with node server.js)
app.listen(3000, function() {
  console.log("server going");
})

routes.js:

module.exports = function (app) {

  app.get('*', function (req, res) {
      res.sendFile(__dirname + '/public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
  });

};

Any help is appreciated :)

like image 900
jSutcliffe90 Avatar asked Aug 31 '16 20:08

jSutcliffe90


1 Answers

I had the same problem and I moved

  app.get('*', function (req, res) {
      res.sendFile(__dirname + '/public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
  });

to server.js right under the require('./app/routes.js')(app); and that fixed it! Because it was looking for index.html in the wrong place when a server-side route was being called. I think you could have also changed the path too but I found this to be clearer.

like image 92
averroes Avatar answered Sep 26 '22 16:09

averroes