admin管理员组

文章数量:1323184

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 mon 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 :)

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 mon 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 :)

Share Improve this question asked Aug 31, 2016 at 20:52 jSutcliffe90jSutcliffe90 3232 gold badges6 silver badges19 bronze badges 3
  • 1 Your public folder isn't inside the app folder in the picture you've posted, so clearly app/public/... is wrong ? – adeneo Commented Aug 31, 2016 at 20:57
  • Yeah that's clearly the issue, showing my inexperience here! Which part of the route is adding the "app" part? It's the same as scotch.io but obviously my request is going to a different directory. Isn't app.get just calling express to make the route, rather than adding it to the path? – jSutcliffe90 Commented Aug 31, 2016 at 21:26
  • Did you setup app.use(express.static(__dirname + '/public')); so express can serve your static files from the public folder.You can also try out instead import path = require('path'); and then use app.use('/', express.static(path.resolve('./public'))); – ramon22 Commented Aug 31, 2016 at 23:47
Add a ment  | 

1 Answer 1

Reset to default 4

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.

本文标签: javascriptNodeAngular Error ENOENT no such file or directoryStack Overflow