使用节点或Express返回JSON的正确方法 [英] Proper way to return JSON using node or Express

查看:205
本文介绍了使用节点或Express返回JSON的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,可以尝试获取以下JSON对象:

So, one can attempt to fetch the following JSON object:

$ curl -i -X GET http://echo.jsontest.com/key/value/anotherKey/anotherValue
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=ISO-8859-1
Date: Wed, 30 Oct 2013 22:19:10 GMT
Server: Google Frontend
Cache-Control: private
Alternate-Protocol: 80:quic,80:quic
Transfer-Encoding: chunked

{
   "anotherKey": "anotherValue",
   "key": "value"
}
$

有没有办法在响应中生成完全相同的身体一个使用节点或快递的服务器?显然,可以设置标题并指出响应的内容类型将是application / json,但是有不同的方式来写/发送对象。我经常使用的是通过使用以下形式的命令:

Is there a way to produce exactly the same body in a response from a server using node or express? Clearly, one can set the headers and indicate that the content-type of the response is going to be "application/json", but then there are different ways to write/send the object. The one that I have seen commonly being used is by using a command of the form:

response.write(JSON.stringify(anObject));

但是,有两点可以争论,就像是问题:

However, this has two points where one could argue as if they were "problems":


  • 我们正在发送一个字符串。

  • 此外,最后没有新的行字符。 li>
  • We are sending a string.
  • Moreover, there is no new line character in the end.

另一个想法是使用命令:

Another idea is to use the command:

response.send(anObject);

这似乎是类似于上面的第一个例子,基于curl的输出发送一个JSON对象。然而,当在终端上再次使用卷曲时,身体的末尾没有新的行字符。那么,实际上如何使用节点或节点/ express在最后添加一个新的行字符来写下这些东西?

This appears to be sending a JSON object based on the output of curl similar to the first example above. However, there is no new line character in the end of the body when curl is again being used on a terminal. So, how can one actually write down something like this with a new line character appended in the end using node or node/express?

推荐答案

p>该响应也是一个字符串,如果要发送响应优化,由于某些尴尬的原因,您可以使用类似于 JSON.stringify(anObject,null,3)

That response is a string too, if you want to send the response prettified, for some awkward reason, you could use something like JSON.stringify(anObject, null, 3)

Content-Type 标题设置为 application / json

var http = require('http');

var app = http.createServer(function(req,res){
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ a: 1 }));
});
app.listen(3000);

// > {"a":1}

优化:

var http = require('http');

var app = http.createServer(function(req,res){
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ a: 1 }, null, 3));
});
app.listen(3000);

// >  {
// >     "a": 1
// >  }

我不太清楚为什么要用换行符终止它,但你可以执行 JSON.stringify(...)+'\\\
'
来实现。

I'm not exactly sure why you want to terminate it with a newline, but you could just do JSON.stringify(...) + '\n' to achieve that.

您可以通过更改选项


'json replacer' JSON替换回调,默认为null

'json replacer' JSON replacer callback, null by default

'json spaces'格式化的JSON响应空间,开发中默认为2,生产中为0

'json spaces' JSON response spaces for formatting, defaults to 2 in development, 0 in production

实际上不建议设置为40

app.set('json spaces', 40);

然后你可以回复一些json。

Then you could just respond with some json.

res.json({ a: 1 });

它将使用'json空格 '配置来美化它。

It'll use the 'json spaces' configuration to prettify it.

这篇关于使用节点或Express返回JSON的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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