bier hier
bier hier

Reputation: 22580

What does rangeError: Invalid status code: 0 mean?

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

Answers (2)

bier hier
bier hier

Reputation: 22580

I figured it out:

res.status(200).send(js);

Works.

Upvotes: 0

TimoStaudinger
TimoStaudinger

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

Related Questions