我在我的节点App中使用express来发送JSON响应:
App.get('/shops', function (req, res) {
res.json(200, {
success: true,
reason: 'data found correctly',
shops: shops
});
}
如果在Chrome浏览器中我转到127.0.0.1:port/shops,我会将响应视为字符串(全部在一行中); 是否可以发送格式化的JSON答案?
之前(使用较新版本的快递)我使用的是
answer = JSON.stringify(answer, null, '\t'); // answer is a js object
res.send(200, answer);
但是现在(使用快递4)即使我尝试了类似的东西,我在浏览器中看到一行中有很多\ n和\ t \ tt;
不知道是否是由于快递版......
答案 0 :(得分:1)
json spaces
设置为JSON输出设置缩进字符。要启用漂亮打印,请对您的应用程序执行以下操作:
var app = express();
app.set('json spaces', ' ');
// ...
截至2014-06-24,Express 4 API目前尚未对此进行记录,但行为可在the source code for res.json
中找到:
var spaces = app.get('json spaces');
var body = JSON.stringify(obj, replacer, spaces);