2016-05-21 23 views
7

Mam zainstalowany „wyrazić” używanie KMP, ja z powodzeniem dostał numer portu nasłuchiwania na 3000. ale po jakimś czasie mam następujący błąd,"TypeError: res.sendStatus nie jest funkcją" dlaczego pojawia się ten błąd po jakimś czasie?

TypeError: res.sendStatus is not a function

Jak wiemy, res.sendStatus(404) jest związane express.but ekspres jest wyraźnie zlokalizowany.

Oto kod źródłowy w app.js

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

app.get('/', function(req, res){ 
    res.send('Hello Worlds'); 
}); 

app.use(function(req, res){ 
    res.sendStatus(404); 
}); 

var server = app.listen(3000, function() { 
    var port = server.address().port; 
console.log('Express server listening on port %s', port); 
}); 

Oto moja pełna moc cmd,

> $ node app.js 
Express server listening on port 3000 
TypeError: res.sendStatus is not a function 
    at Object.handle (I:\mongoUniversity\hello_world_templates\app.js:14:9) 
    at next (I:\mongoUniversity\hello_world_templates\node_modules\connect\lib\proto.js:174:15) 
    at pass (I:\mongoUniversity\hello_world_templates\node_modules\express\lib\router\index.js:110:24) 
    at Router._dispatch (I:\mongoUniversity\hello_world_templates\node_modules\express\lib\router\index.js:173:5) 
    at Object.router (I:\mongoUniversity\hello_world_templates\node_modules\express\lib\router\index.js:33:10) 
    at next (I:\mongoUniversity\hello_world_templates\node_modules\connect\lib\proto.js:174:15) 
    at Object.expressInit [as handle] (I:\mongoUniversity\hello_world_templates\node_modules\express\lib\middleware.js:30:5) 
    at next (I:\mongoUniversity\hello_world_templates\node_modules\connect\lib\proto.js:174:15) 
    at Object.query [as handle] (I:\mongoUniversity\hello_world_templates\node_modules\connect\lib\middleware\query.js:43:5) 
    at next (I:\mongoUniversity\hello_world_templates\node_modules\connect\lib\proto.js:174:15) 
    at Function.app.handle (I:\mongoUniversity\hello_world_templates\node_modules\connect\lib\proto.js:182:3) 
    at Server.app (I:\mongoUniversity\hello_world_templates\node_modules\connect\lib\connect.js:67:37) 
    at emitTwo (events.js:87:13) 
    at Server.emit (events.js:172:7) 
    at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:525:12) 
    at HTTPParser.parserOnHeadersComplete (_http_common.js:88:23) 

Sprawdziłem podobne pytania, gdzie kazali sprawdzić, czy „wyrazić” został zainstalowany lub nie, ale zainstalowałem go poprawnie. Więc co robię źle?

+1

Sprawdź wersję ekspresu, którego używasz. Tylko '' 4.x' używa 'res.sendStatus'. –

+0

Naprawiono !, Dzięki @PatrickRoberts Znalazłem, że w pliku "package.json" zdefiniowana wersja ekspresowa była 3.x (zaktualizowana teraz), ale ja także zainstalowałem globalnie wersję ekspresową 4.13.4, więc gdy sprawdziłem wersję Express na komendę linia zawsze zwracała wersję 4.x, więc mniej martwiłem się wersją. –

+0

Express nie powinien być instalowany globalnie, więc staraj się tego unikać. –

Odpowiedz