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

问题描述

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

$ curl -i -X GET http://echo.jsontest.com/key/value/anotherKey/anotherValueHTTP/1.1 200 正常访问控制允许来源:*内容类型:应用程序/json;字符集=ISO-8859-1日期:2013 年 10 月 30 日,星期三 22:19:10 GMT服务器:谷歌前端缓存控制:私有备用协议:80:quic,80:quic传输编码:分块{"anotherKey": "anotherValue",核心价值"}$

有没有办法在使用 node 或 express 的服务器的响应中生成完全相同的正文?显然,可以设置标头并指示响应的内容类型将是application/json",但是写入/发送对象有不同的方法.我看到常用的一种是使用以下形式的命令:

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

然而,这有两点可以让人认为它们是问题":

  • 我们正在发送一个字符串.
  • 此外,最后没有换行符.

另一个想法是使用命令:

response.send(anObject);

这似乎是基于 curl 的输出发送一个 JSON 对象,类似于上面的第一个示例.但是,当 curl 再次在终端上使用时,正文末尾没有换行符.那么,如何使用 node 或 node/express 在末尾附加一个换行符来真正写下这样的东西?

解决方案

那个响应也是一个字符串,如果你想发送美化的响应,出于一些尴尬的原因,你可以使用类似 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.end(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.end(JSON.stringify({ a: 1 }, null, 3));});app.listen(3000);//>{//>一个":1//>}

我不确定您为什么要用换行符终止它,但您可以执行 JSON.stringify(...) + '' 来实现这一点.>

快递

在 express 中,您可以通过更改选项来实现.

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

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

实际上不建议设置为 40

app.set('json 空格', 40);

然后你可以用一些 json 来回应.

res.json({ a: 1 });

它将使用 '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"
}
$

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":

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

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?

解决方案

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)

It's important that you set the Content-Type header to application/json, too.

var http = require('http');

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

// > {"a":1}

Prettified:

var http = require('http');

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

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

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

Express

In express you can do this by changing the options instead.

Not actually recommended to set to 40

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

Then you could just respond with some json.

res.json({ a: 1 });

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

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

07-26 04:42