node.js - 无法读取 finalHandler Node 模块中未定义的属性 '_header'

标签 node.js node-modules

我似乎找不到解决问题的方法。我正在尝试启动我的 nodeJS/expressJS 应用程序,但在“npm 启动”时收到错误消息。

错误和堆栈跟踪:

    /Users/jmccreary/Documents/NewHireSite/node_modules/finalhandler/index.js:94
    if (!err && res._header) {
                   ^

TypeError: Cannot read property '_header' of undefined
    at /Users/jmccreary/Documents/NewHireSite/node_modules/finalhandler/index.js:94:20
    at /Users/jmccreary/Documents/NewHireSite/node_modules/express/lib/router/index.js:635:15
    at next (/Users/jmccreary/Documents/NewHireSite/node_modules/express/lib/router/index.js:210:14)
    at Function.handle (/Users/jmccreary/Documents/NewHireSite/node_modules/express/lib/router/index.js:174:3)
    at Function.handle (/Users/jmccreary/Documents/NewHireSite/node_modules/express/lib/application.js:174:10)
    at app (/Users/jmccreary/Documents/NewHireSite/node_modules/express/lib/express.js:38:9)
    at Object.<anonymous> (/Users/jmccreary/Documents/NewHireSite/app.js:101:25)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/jmccreary/Documents/NewHireSite/bin/www:7:11)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)

违规代码(在 node_modules/finalhandler/index.js 中):
(注意:我自己添加了 console.logs,它们不是模块的一部分)

  function finalhandler (req, res, options) {
  var opts = options || {}
  console.log("The req is: ", req);
  console.log("The res is: ", res);

  // get environment
  var env = opts.env || process.env.NODE_ENV || 'development'

  // get error callback
  var onerror = opts.onerror

  return function (err) {
    var headers
    var msg
    var status

    // ignore 404 on in-flight response
    if (!err && res._header) {
      debug('cannot 404 after headers sent')
      return
    }
    ...

我的 console.log() 的输出如下所示:

 The req is:  Authenticator {
  _key: 'passport',
  _strategies: { session: SessionStrategy { name: 'session' } },
  _serializers: [],
  _deserializers: [],
  _infoTransformers: [],
  _framework: 
   { initialize: [Function: initialize],
     authenticate: [Function: authenticate] },
  _userProperty: 'user',
  Authenticator: [Function: Authenticator],
  Passport: [Function: Authenticator],
  Strategy: { [Function: Strategy] Strategy: [Circular] },
  strategies: { SessionStrategy: { [Function: SessionStrategy] super_: [Object] } } }
 The res is:  undefined

编辑:我认为这是调用错误的代码(来自 node_modules/express/lib/application.js):

 /**
 * Dispatch a req, res pair into the application. Starts pipeline processing.
 *
 * If no callback is provided, then default error handlers will respond
 * in the event of an error bubbling through the stack.
 *
 * @private
 */

app.handle = function handle(req, res, callback) {
  var router = this._router;

  // final handler
  var done = callback || finalhandler(req, res, {
    env: this.get('env'),
    onerror: logerror.bind(this)
  });

  // no routes
  if (!router) {
    debug('no routes defined on app');
    done();
    return;
  }

  router.handle(req, res, done);
};

我的 package.json 文件(使用 Express 4)如下:

{
  "name": "newhiresite",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
"dependencies": {
    "body-parser": "~1.17.1",
    "connect-flash": "^0.1.1",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.3",
    "express": "~4.15.2",
    "express-session": "^1.15.3",
    "express-validator": "^3.2.0",
    "fs": "0.0.1-security",
    "mongodb": "^2.2.25",
    "mongoose": "^4.10.1",
    "morgan": "~1.8.1",
    "node-sass-middleware": "0.9.8",
    "passport": "~0.3.2",
    "passport-local": "~1.0.0",
    "pug": "~2.0.0-rc.1",
    "serve-favicon": "~2.4.2"
  }
}

我能够很好地运行教程的源代码,它具有与我的应用程序相同的模块,几乎完全相同(除了 npm start 命令上的 www/bin 文件)。我已尝试删除其他模块并在我的 app.js 文件中四处移动内容,但均无济于事。

任何指点或帮助将不胜感激。谢谢!

最佳答案

我有同样的错误。以下解决了我的问题,也许对你有用?我有这段代码:

var express = require('express')();
var app = express();

app.listen(3000, function () {
  console.log('Example app listening at http://%s:%s');
});

它有一个 () 太多了。删除它解决了问题:

var express = require('express');

关于node.js - 无法读取 finalHandler Node 模块中未定义的属性 '_header',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44267191/

相关文章:

node.js - webpack 捆绑针对 node.js 的代码

node.js - 通过 Angular App 和 NodeJS 发送 GET/POST 请求到 express 服务器时如何修复 404 Not Found

node.js - 如何让nodemon在崩溃时退出?

javascript - 如何通过 npm 正确导入 jquery 验证

javascript - 需要中间件功能的路由

javascript - 无法在nodejs中为URL.createObjectURL添加polyfill

javascript - 任何人都可以帮我计算快速端口随机发生器的数学吗

node.js - 获取 TypeError : wsc. require is not a function for hyco-ws npm package

node.js - angular-cli:为什么当 npm 安装新模块时,旧模块会被删除

aws-lambda - 安装 "sharp"模块时出错 (AWS Lambda)