gpt4 book ai didi

node.js - Hapi - 仅回复 JSON

转载 作者:搜寻专家 更新时间:2023-10-31 23:29:08 25 4
gpt4 key购买 nike

如何调整 Hapi 回复功能,使其只回复 JSON 对象?我应该把它原样发送然后发送吗?好像没找到好的例子

这里是一些编辑 - 添加了一些示例代码以了解正在发生的事情。

路线:

server.route({
method: 'GET',
path: '/user/',
handler: function (request, reply) {

var ids = null;

mysqlConnection.query('SELECT ID FROM Users;',function(err,rows,fields){
if(err) throw err;
ids = rows;
// console.log(ids);
reply(ids);
});
}
});

回复:

<html><head></head><body>
<pre style="word-wrap: break-word; white-space: pre-wrap;">[{"ID":1},{"ID":2},{"ID":3},{"ID":4},{"ID":5},{"ID":6},{"ID":7},{"ID":8},{"ID":9},{"ID":10},{"ID":11},{"ID":12},{"ID":13},{"ID":14},{"ID":15},{"ID":16},{"ID":17},{"ID":18},{"ID":19},{"ID":20},{"ID":21}]
</pre></body></html>

最佳答案

我希望我能理解这个问题。我们在谈论 8.x 版吗?对我来说,这似乎是默认设置。使用此代码作为路由处理程序,

folders: {
handler: function( request, reply ) {

'use strict';
reply({
folders: folders
}).code( 200 );

}
},

和做

curl http://localhost:3001/folders

我得到以下输出

* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3001 (#0)
> GET /folders HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:3001
> Accept: */*
>
< HTTP/1.1 200 OK
< content-type: application/json; charset=utf-8
< cache-control: no-cache
< content-length: 266
< accept-ranges: bytes
< Date: Tue, 03 Feb 2015 23:19:31 GMT
< Connection: keep-alive
<
{folders ..... }

另外,请注意我只调用了 reply() 而不是 return reply()

HTH

关于node.js - Hapi - 仅回复 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28307962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com