Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following code:

var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');

var port = process.env.port || 1337;

var app = express();
app.use(bodyParser.urlencoded({ 
    extended: true
}));

app.get('/', function (request, response) {
    response.sendFile(__dirname + "/views/MoviesList.html");
});

app.get('/api/list', function (request, response) {
    response.status(200).json(
        { "movieId": 1, "name": "The Pacific Rim" },
        { "movieId": 2, "name": "Transformer" });
});

app.listen(port);


I am getting following error in browser when I visit "http://localhost:1337/api/list"

"RangeError: Invalid status code: 0"

It also gave warning in node that res.json(status, obj) is deprecated use res.status(200).json(obj) instead. What mistake I am making here? Can anyone please help?

What I have tried:

I tried many solution provided from codeproject, solution was implemented as per codeproject posts, but still I am getting above error & deprecated message.
Posted
Updated 9-Aug-16 23:36pm

1 solution

Please open local host private browser pr
had a similar error message just now and managed to solve the problem by changing:
res.status(statusCode);

to:
if (statusCode >= 100 && statusCode < 600)
res.status(statusCode);
else
res.status(500);

or just:
res.status(statusCode >= 100 && statusCode < 600 ? err.code : 500);

I.e. make sure you aren't trying to set an invalid HTTP status code somewhere.

It's likely this is the issue but it looks like you've accidentally duplicated the app.js code instead of pasting the edit.js code in the question.
 
Share this answer
 
Comments
Developer_Independant 10-Aug-16 5:38am    
I have change the code like this

response.status(response.statusCode >= 100 && response.statusCode < 600 ? err.code : 500).json(
{ "movieId": 1, "name": "The Pacific Rim" },
{ "movieId": 2, "name": "Yeh Jawani Hai Deewani" });

Its still end up landing on err.code result
Malil Developers 10-Aug-16 6:02am    
Please add full codes and all files codes for batter answer
Developer_Independant 10-Aug-16 6:21am    
<pre lang="Javascript">var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');

var port = process.env.port || 1337;

var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));

app.get('/', function (request, response) {
response.sendFile(__dirname + "/views/MoviesList.html");
});

app.get('/api/list', function (request, response) {
response.status(response.statusCode >= 100 && response.statusCode < 600 ? err.code : 500).json(
{ "movieId": 1, "name": "The Pacific Rim" },
{ "movieId": 2, "name": "Yeh Jawani Hai Deewani" });
});

app.listen(port);</pre>
Developer_Independant 10-Aug-16 6:36am    
its simple nodejs web application with npm express, body-parser installed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900