因此,可以尝试获取以下JSON对象:
$ 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"
}
$
有没有产生相同的身体使用节点或表达服务器的响应的方法吗? 显然,一个可以设置标题和指示内容类型的响应将是“应用/ JSON”,但也有许多不同的方式来编写/发送的对象。 我所看到的通常所使用的一种是通过使用以下形式的指令:
response.write(JSON.stringify(anObject));
然而,这两个点,如果他们的“问题”人们可以说:
另一个想法是使用下面的命令:
response.send(anObject);
这似乎基于的卷曲的输出类似于以上第一个例子中被发送JSON对象。 然而,在本体的端部没有新行字符时卷曲再次被在终端上使用。 那么,怎样才能人真正写下这样的事情,在使用节点或节点末尾追加新的行字符/表达什么?
这种反应是一个字符串也一样,如果你想发送的响应美化,对一些尴尬的原因,你可以使用类似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);
// > {
// > "a": 1
// > }
我不知道是什么原因,你想用一个新行终止它,但你可以只是做JSON.stringify(...) + '\n'
来实现这一目标。
表达
在快递,你可以通过做这个改变的选项,而不是 。
'json replacer'
JSON替代品回调,默认为null
'json spaces'
JSON响应空间进行格式化,在生产默认为2的开发,0
实际上并不建议设置40
app.set('json spaces', 40);
然后,你可以只用一些JSON响应。
res.json({ a: 1 });
它会使用'json spaces
”配置美化它。
由于Express.js 3X响应对象具有正确设置所有的头,为您和返回JSON格式的响应JSON()方法。
例:
res.json({"foo": "bar"});
如果你想送您可以使用流的JSON文件
var usersFilePath = path.join(__dirname, 'users.min.json');
apiRouter.get('/users', function(req, res){
var readable = fs.createReadStream(usersFilePath);
readable.pipe(res);
});
您可以使用管道和多处理器的一个只是美化它。 您的应用程序应始终以尽可能小的负载尽可能的响应。
$ curl -i -X GET http://echo.jsontest.com/key/value/anotherKey/anotherValue | underscore print
https://github.com/ddopson/underscore-cli
您可以使用中间件来设置默认的内容类型,并针对不同的API特别设置的Content-Type。 下面是一个例子:
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const server = app.listen(port);
server.timeout = 1000 * 60 * 10; // 10 minutes
// Use middleware to set the default Content-Type
app.use(function (req, res, next) {
res.header('Content-Type', 'application/json');
next();
});
app.get('/api/endpoint1', (req, res) => {
res.send(JSON.stringify({value: 1}));
})
app.get('/api/endpoint2', (req, res) => {
// Set Content-Type differently for this particular API
res.set({'Content-Type': 'application/xml'});
res.send(`<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>`);
})
快递使用旧版本app.use(express.json())
或bodyParser.json()
阅读更多关于bodyParser中间件
在快递,我们可以简单地使用最新版本的res.json()
const express = require('express'),
port = process.env.port || 3000,
app = express()
app.get('/', (req, res) => res.json({key: "value"}))
app.listen(port, () => console.log(`Server start at ${port}`))
如果你就像使用快递u可以使用这样的:
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({key:"value"}));
或者这只是
res.json({key:"value"});