【问题标题】:Node, Angular Error: ENOENT: no such file or directory节点,角度错误:ENOENT:没有这样的文件或目录
【发布时间】:2016-09-09 12:11:22
【问题描述】:

我一直在按照a scotch.io 教程创建我的第一个节点和角度应用程序。我已经看到相对路径是Error: ENOENT: no such file or directory 在线的一个常见问题,据我所知,它与教程相同,所以我就是为什么它不起作用。

完整的错误信息是: Error: ENOENT: no such file or directory, stat'/Users/badman/githubRepos/travelGuide/app/public/index.html' at Error (native)

My folder structure is here. 我的 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)
  });

};

任何帮助表示赞赏:)

【问题讨论】:

  • 您的public 文件夹不在您发布的图片中的app 文件夹内,所以很明显app/public/... 是错误的?
  • 是的,这显然是问题所在,这表明我在这里缺乏经验!路线的哪个部分添加了“应用程序”部分?它与 scotch.io 相同,但显然我的请求将转到不同的目录。 app.get 不是只是调用 express 来做路由,而不是添加到路径中吗?
  • 你设置了 app.use(express.static(__dirname + '/public'));所以 express 可以从公共文件夹中提供你的静态文件。你也可以试试 import path = require('path');然后使用 app.use('/', express.static(path.resolve('./public')));

标签: javascript angularjs node.js


【解决方案1】:

我遇到了同样的问题,我搬家了

  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)
  });

到require('./app/routes.js')(app); 正下方的server.js 并修复它!因为当调用服务器端路由时,它在错误的位置寻找 index.html。我认为您也可以更改路径,但我发现这更清晰。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2019-03-09
    • 2017-12-28
    • 2022-01-16
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多