Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"TypeError: res.sendStatus is not a function" why I am getting this error after sometime?

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.parserOnIncoming [as onIncoming] (_http_server.js:525:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.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?

like image 820
Md. Nahiduzzaman Rose Avatar asked May 21 '16 16:05

Md. Nahiduzzaman Rose


1 Answers

Use express 4.x, since only the new express API supports res.sendStatus.

like image 151
Patrick Roberts Avatar answered Sep 17 '22 14:09

Patrick Roberts