1

if i give error message like this it working fine without problem

res.status(401).send('not found, please register');

but when i change to this one i am getting error

return res.status('EXTINF-10202').json({
                'message': 'not found, please register'
            });

i am getting error like this

F:\test\node_modules\mongoose\node_modules\mongodb\lib\utils.js:97
    process.nextTick(function() { throw err; });
                                  ^

RangeError: Invalid status code: 0
    at ServerResponse.writeHead (_http_server.js:192:11)
    at ServerResponse.writeHead (F:\test\node_modules\on-headers\index.js:
55:19)
    at ServerResponse.res.writeHead (F:\test\node_modules\express-session\
index.js:169:17)
    at ServerResponse._implicitHeader (_http_server.js:157:8)
    at ServerResponse.res.write (F:\test\node_modules\compression\index.js
:90:14)
    at ServerResponse.res.end (F:\test\node_modules\compression\index.js:1
11:14)
    at F:\test\node_modules\express-session\index.js:182:13
    at F:\test\node_modules\connect-mongo\lib\connect-mongo.js:365:9
    at handleCallback (F:\test\node_modules\mongoose\node_modules\mongodb\
lib\utils.js:95:12)
    at F:\test\node_modules\mongoose\node_modules\mongodb\lib\collection.j
s:666:5
    at F:\test\node_modules\mongoose\node_modules\mongodb-core\lib\topolog
ies\server.js:795:13
    at Callbacks.emit (F:\test\node_modules\mongoose\node_modules\mongodb-
core\lib\topologies\server.js:94:3)
    at Connection.messageHandler (F:\test\node_modules\mongoose\node_modul
es\mongodb-core\lib\topologies\server.js:235:23)
    at Socket.<anonymous> (F:\test\node_modules\mongoose\node_modules\mong
odb-core\lib\connection\connection.js:259:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
its me
  • 524
  • 6
  • 28
  • 62

1 Answers1

3

If you take a look at the ExpressJS source for res.status you will see that the function expects a Number, not a String:

/**
 * Set status `code`.
 *
 * @param {Number} code
 * @return {ServerResponse}
 * @public
 */

res.status = function status(code) {
  this.statusCode = code;
  return this;
};

https://github.com/expressjs/express/blob/9722202df964bfbfc0f579e4baeb5a4e1b43b344/lib/response.js#L57-L68

Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • what can i do now .. if i want to add this 'EXTINF-10202' status code – its me Mar 21 '17 at 06:16
  • That is not a valid HTTP status code, you would be better off sending the code in your JSON: `res.json({ code: ''EXTINF-10202'', message: 'not found, please register' })` – Rob M. Mar 21 '17 at 06:17
  • ok i understood but is it possible to give return res.status('EXTINF-10202').json({ 'message': 'not found, please register' }); – its me Mar 21 '17 at 06:25
  • Sorry, it is not possible, because that is not a valid HTTP status code that browsers will understand - there is a limited set of valid HTTP status codes that you can use. – Rob M. Mar 21 '17 at 06:31