rangeError:无效的状态代码:0是什么意思? [英] What does rangeError: Invalid status code: 0 mean?

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

问题描述

我正在努力表达快递,并拥有以下get方法:

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

当我注释掉JSON.parse语句并将js替换为一些有效的json时,它没有问题吗?我该如何解决?

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?

推荐答案

在这种情况下,无需 JSON.parse().该函数调用不正确,因为该函数需要一个字符串,而不是对象.

No need to JSON.parse() in this situation. The function call done incorrectly, since the function expects a string, not an object.

这应该可以正常工作:

app.get('/myendpoint', function(req, res) {
    res.status(200).json({code: 'success', message:'Valid'});
});

Express文档中的更多信息.

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

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