Rangeerror:无效的状态代码:0 [英] Rangeerror: invalid status code: 0

查看:182
本文介绍了Rangeerror:无效的状态代码:0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:



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);





当我访问http时,我在浏览器中收到以下错误:// localhost:1337 / api / list



RangeError:无效状态代码:0



它还在节点中发出警告: res.json(status,obj)已弃用,请使用res.status(200).json(obj)。我在这里弄错了什么?有人可以帮忙吗?



我尝试过:



我尝试了许多来自codeproject的解决方案,解决方案是按照代码项目的帖子实现的,但我仍然遇到了错误&已弃用的消息。



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.

推荐答案

请打开本地主机私人浏览器pr

刚才有类似的错误消息并设法通过更改解决问题:

res.status(statusCode);



to:

if(statusCode> = 100& ;& statusCode< 600)

res.status(statusCode);

else

res.status(500);



或只是:

res.status(statusCode> = 100&& statusCode< 600?err.code:500); < br $> b $ b

即确保你没有尝试在某处设置无效的HTTP状态代码。



这可能是问题,但看起来你不小心重复了应用程序。 js代码而不是粘贴问题中的edit.js代码。
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.


这篇关于Rangeerror:无效的状态代码:0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆