2017-05-30 46 views
2

我似乎無法找到解決我的問題的方法。我試圖啓動我的nodeJS/expressJS應用程序,但在「npm start」時收到錯誤。無法讀取finalHandler節點模塊中未定義的屬性'_header'

的錯誤&堆棧跟蹤:

/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 /快遞/ 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); 
}; 

我的包。 JSON文件(使用快遞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啓動命令後的www/bin文件外)。我嘗試刪除其他模塊,並在app.js文件中移動,無濟於事。

任何指針或幫助將不勝感激。謝謝!

+0

缺少最重要的部分:您使用'finalhandler'的代碼。 – robertklep

+0

我相信我加了正確的代碼。這是我的第一個NodeJS應用程序,所以如果這是不正確的,請讓我知道你相信可能會調用它。 –

+0

你的代碼中有一個地方叫'finalhandler(...)'。你應該發佈該代碼。 – robertklep

回答

0

我有同樣的錯誤。以下解決了我的問題,也許它適合你?我有這樣的代碼:

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

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

它有一個()太多。刪除它解決了這個問題:

var express = require('express'); 
+0

'來獲得解決方案'()'你如何看待這個問題的答案? 沒有,我從你的答案中獲得幫助。 –

相關問題