Reputation: 22580
I am getting my head around express and have this get method:
app.get('/myendpoint', function(req, res) {
var js= JSON.parse ({code: 'success', message:'Valid'});
res.status(200).json(js);
});
When this endpoint is hit I get an error:
_http_server.js:192
throw new RangeError(`Invalid status code: ${statusCode}`);
When I comment out the JSON.parse statement and replace the js for some valid json it has no issues? How can I fix this?
Upvotes: 0
Views: 5660
Reputation: 42520
No need to JSON.parse()
in this situation. The function call done incorrectly, since the function expects a string, not an object.
This should work just fine:
app.get('/myendpoint', function(req, res) {
res.status(200).json({code: 'success', message:'Valid'});
});
More info in the Express docs.
Upvotes: 1