错误:ENOENT:没有Angular2和Express.js这样的文件或目录

时间:2016-07-28 00:57:35

标签: javascript angularjs node.js express angular

我正在对this sample Angular2 app on GitHub进行少量修改,以便它使用Express.js而不是KOA。但是,当我尝试在FireFox中加载应用程序时,nodemon控制台中会打印以下错误:

Error: ENOENT: no such file or directory

localhost : 8080的http请求触发*路由器处理程序时,Angular2应用程序开始加载,该处理程序返回index.html,然后触发一系列嵌套依赖项的回调,其中一个它会抛出错误并在中途停止应用程序加载。

需要对GitHub示例中的代码进行哪些具体更改才能解析Error: ENOENT: no such file or directory


背景和代码:
以下是router.js的新替代品,它使用Express.js代替KOA:

'use strict';
let uuid = require('node-uuid');
var path = require('path');
let jwt = require('jsonwebtoken');
let config = require('./config');

// expose the routes to our app with module.exports
module.exports = function(app) {

    //all other methods omitted here for brevity

    app.get('*', function(req, res) {
        console.log('inside * route!');           
        if(req.url === '/'){
            res.sendFile(path.resolve('dist/client/index.html')); // load the single view file (angular will handle the front-end)
        } else {
            res.sendFile(path.resolve('./dist/client' + req.url));
        }
    });
};

针对nodemon的请求的localhost : 8080控制台输出会反复触发上述Express.js app.get('*'...)方法导致错误:

App listening on port 8080
inside * route!
GET / 304 54.261 ms - -
inside * route!
inside * route!
inside * route!
GET /boot.css 304 4.637 ms - -
GET /boot.js 304 4.447 ms - -
GET /vendor.js 304 3.742 ms - -
inside * route!
GET /vendor.js.map 200 3.180 ms - 3115631
inside * route!
GET /boot.js.map 200 2.366 ms - 61810
inside * route!
GET /bootstrap.css.map 404 2.515 ms - 169
Error: ENOENT: no such file or directory, stat '/home/user/nodejs_apps/angular2_oauth_seed_app/dist/client/bootstrap.css.map'
    at Error (native)

指定Express.js的新server/index.js是:

// set up ======================================================================
var express  = require('express');
var app      = express();                               // create our app w/ express
var port     = process.env.PORT || 8080;                // set the port
var morgan = require('morgan');             // log requests to the console (express4)
var bodyParser = require('body-parser');    // pull information from HTML POST (express4)
var methodOverride = require('method-override'); // simulate DELETE and PUT (express4)

app.use('/static', express.static(__dirname + '/dist/client'));
app.use(morgan('dev'));                                         // log every request to the console
app.use(bodyParser.urlencoded({'extended':'true'}));            // parse application/x-www-form-urlencoded
app.use(bodyParser.json());                                     // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(methodOverride());
app.use('/scripts', express.static(__dirname + '/node_modules/'));

// load the routes
require('./router')(app);

// listen (start app with node server.js) ======================================
app.listen(port);
console.log("App listening on port " + port);

GitHub示例应用程序中的其他所有内容与您在GitHub上看到的内容相同package.json除外,router.js仅包含支持index.js中的更改的依赖项和csv如上所示。

1 个答案:

答案 0 :(得分:1)

bootstrap.css.map表示您的浏览器正在尝试为Bootstrap CSS加载源映射(这是一个调试工具)。

这只会在您打开浏览器的开发人员工具时发生,因此不是严格意义上的问题(除非您想实际调试Bootstrap的CSS)。除此之外,它总是可以请求其他URL,并产生相同的错误。

解决方案是向sendFile添加显式错误处理程序,并假设发生错误时,因为文件不存在(因此它应该产生404响应) ):

res.sendFile(path.resolve('./dist/client' + req.url), (err) => {
  if (err) return res.sendStatus(404);
});