node.js node-express 错误:express 弃用 res.send(status):改用 res.sendStatus(status)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30550049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:49:03  来源:igfitidea点击:

node-express error : express deprecated res.send(status): Use res.sendStatus(status) instead

node.jsexpressresponse

提问by D-W-A

I am trying to send an integer via response.send()but I keep getting this error

我正在尝试通过发送一个整数,response.send()但我不断收到此错误

express deprecated res.send(status): Use res.sendStatus(status) instead

表示不推荐使用的 res.send(status):使用 res.sendStatus(status) 代替

I am not sending a Status, my code is

我没有发送状态,我的代码是

app.get('/runSyncTest' , function(request, response){  

var nodes = request.query.nodes;
var edges = request.query.edges;
if (edges == "" ){
    edges = []
}

userStory.userStory(nodes,edges);
connection.query('SELECT MAX(id) as id FROM report ', function(err,results, fields) {
                idTest = results[0].id
                response.send (idTest)
});

});

回答by Donskikh Andrei

You could try this:

你可以试试这个:

res.status(200).send((results[0].id).toString());

Guys are right - it doesn't allow numbers. Prooflink: http://expressjs.com/4x/api.html#res.send

伙计们是对的 - 它不允许数字。校对链接:http://expressjs.com/4x/api.html#res.send

回答by Jerome Miranda

This is because you are sending numeric value in the res.send.

这是因为您在 res.send 中发送数值。

You could send a json object or convert it to string.

您可以发送一个 json 对象或将其转换为字符串。

回答by robertklep

(as mentioned in the comments already)

(如评论中所述)

The manual states:

该手册指出

The body parameter can be a Buffer object, a String, an object, or an Array.

body 参数可以是一个 Buffer 对象、一个字符串、一个对象或一个数组。

So integers aren't directly supported and need to be converted to one of those types first. For instance:

因此,不直接支持整数,需要先将其转换为其中一种类型。例如:

response.send(String(idTest));

回答by Anshul Bisht

Use like this,

像这样使用,

res.status(404).send('Page Not found');

回答by RegarBoy

As long as you're not sending String or Object/Array data you get an error. Solution convert your data to string:

只要您不发送字符串或对象/数组数据,就会出现错误。解决方案将您的数据转换为字符串:

app.get('/runSyncTest', function(req, res) {
    var number = 5000;
    res.send((number).toString()); //Number is converted with toString()
});

回答by Dere Sagar

Its deprecated Express 5 no longer supports the signature like-

其已弃用的 Express 5 不再支持这样的签名-

res.json(200, {
   result: result
});

Instead, use a method like this, means you only need to change the format of sending responses.

相反,使用这样的方法,意味着您只需要更改发送响应的格式。

res.status(status).json(obj)

Example -

例子 -

res.status(200).json({'success' : true, 'result': 'result'})