最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Hapi

IT培训 admin 7浏览 0评论

Hapi

如何调整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()not return reply()

HTH

Hapi

如何调整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()not return reply()

HTH

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论