admin管理员组文章数量:1341390
I have installed 'express' using npm, I've successfully got port number listening on 3000. but after a while i got the following error,
TypeError: res.sendStatus is not a function
As we know, res.sendStatus(404)
is related to express.but express is clearly located.
Here is source code in 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);
});
Here is my full cmd output,
> $ 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.parserOnIning [as onIning] (_http_server.js:525:12)
at HTTPParser.parserOnHeadersComplete (_http_mon.js:88:23)
I have checked similar questions, where they told to check if 'express' was installed or not, but i have installed it correctly. Then what am i doing wrong?
I have installed 'express' using npm, I've successfully got port number listening on 3000. but after a while i got the following error,
TypeError: res.sendStatus is not a function
As we know, res.sendStatus(404)
is related to express.but express is clearly located.
Here is source code in 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);
});
Here is my full cmd output,
> $ 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.parserOnIning [as onIning] (_http_server.js:525:12)
at HTTPParser.parserOnHeadersComplete (_http_mon.js:88:23)
I have checked similar questions, where they told to check if 'express' was installed or not, but i have installed it correctly. Then what am i doing wrong?
Share edited Jun 12, 2017 at 5:23 Md. Nahiduzzaman Rose asked May 21, 2016 at 16:33 Md. Nahiduzzaman RoseMd. Nahiduzzaman Rose 4422 gold badges10 silver badges24 bronze badges 3-
2
Check the version of express you're using. Only
4.x
usesres.sendStatus
. – Patrick Roberts Commented May 21, 2016 at 16:42 - Got fixed!, Thanks @PatrickRoberts I found that in "package.json" file the defined express version was 3.x (updated now), but i also globally installed express 4.13.4 version, so when i checked express version on mand line it always returned 4.x version, so i was less worried about the version. – Md. Nahiduzzaman Rose Commented May 21, 2016 at 16:55
- Express is not supposed to be installed globally, so try to avoid doing that. – Patrick Roberts Commented May 30, 2016 at 15:13
2 Answers
Reset to default 8Use express 4.x
, since only the new express API supports res.sendStatus
.
In my case, the parameters passed to the function was the issue. Adding next as as a parameter fixed it
app.use(function(req, res, next){
res.sendStatus(404);
});
本文标签:
版权声明:本文标题:javascript - "TypeError: res.sendStatus is not a function" why I am getting this error after sometime? - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743673746a2519963.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论