N.Nicolas N.Nicolas - 3 months ago 129
Node.js Question

throw new RangeError node js

I'm trying to work with IBM Watson Conversation service with Node.js.
I use 'express' to post a message:

app.post( '/api/message', function(req, res) {
}


and to print the message got from the service:

conversation.message( payload, function(err, data) {
if ( err ) {
return res.status( err.code || 500 ).json( err );
}
return res.json( updateMessage( payload, data ) );
} );


I just ran the application on port 3000. While the page is not loaded and I got this error:

_http_server.js:192
throw new RangeError(`Invalid status code: ${statusCode}`);
^

RangeError: Invalid status code: 0
at ServerResponse.writeHead (_http_server.js:192:11)
at ServerResponse._implicitHeader (_http_server.js:157:8)
at ServerResponse.OutgoingMessage.end (_http_outgoing.js:573:10)
at ServerResponse.send (C:\IBM\1.Mission\2016\conversation-simple-master(1)\
conversation-simple-master\node_modules\express\lib\response.js:204:10)
at ServerResponse.json (C:\IBM\1.Mission\2016\conversation-simple-master(1)\
conversation-simple-master\node_modules\express\lib\response.js:249:15)
at C:\IBM\1.Mission\2016\conversation-simple-master(1)\conversation-simple-m
aster\app.js:86:44
at Request._callback (C:\IBM\1.Mission\2016\conversation-simple-master(1)\co
nversation-simple-master\node_modules\watson-developer-cloud\lib\requestwrapper.
js:47:7)
at self.callback (C:\IBM\1.Mission\2016\conversation-simple-master(1)\conver
sation-simple-master\node_modules\watson-developer-cloud\node_modules\request\re
quest.js:200:22)
at emitOne (events.js:77:13)
at Request.emit (events.js:169:7)


I don't think the problem is from npm, back my package... While it seems a generic problem...Thanks for you help.

qzb qzb
Answer

Request to IBM Watson Conversation service probably ended with error with code "0" and it isn't a valid HTTP status code. This should work:

conversation.message(payload, function(err, data) {
  if (err) {
    return res.status(500).json(err);
  }

  return res.json(updateMessage(payload, data));
});