node.js - 如何将 url 查询参数添加到 hapijs 中的 hapi 代理调用中

标签 node.js hapi.js

假设我想在这次对 google 的调用中添加一个查询参数,但对其进行编码,而不是从 http 调用进行转发

我正在使用nodejs hapi框架

server.route({ 
    method: '*', 
    path: '/{p*}', 
    handler: { 
        proxy: { host: 'google.com', port: 80, redirects: 5 } 
    } 
});

可以在调用之前修改 url。

最佳答案

我通过代理做到了这一点:mapUri

server.route({ 
    method: 'GET', 
    path: '/index/search/{p}', 
    handler: {
    proxy: {
            mapUri:  function (request, callback) {
        query = request.url.search ? request.url.search : '';
        p = request.params.p ? '.' + request.params.p +'.' : '' ;

        //loaded from a configuration file
        tls = Config..tls;
        host = Config..host; 
        port = Config.port;

        url = (tls ? 'https://' : 'http://') + host + port + p  + request.path + query;

        console.log('Method: ' + request.method.toUpperCase() + ' Url: ' + url);
        callback(null,url);
        }
    }
    }
});

关于node.js - 如何将 url 查询参数添加到 hapijs 中的 hapi 代理调用中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21745360/

相关文章:

javascript - 使用搜索索引过滤

node.js - NGINX子域301重定向

javascript - 我应该如何使用 NodeJS 包含我的自定义记录器?

hapi.js - hapi 服务器上的多个连接

javascript - Hapi 处理程序方法未返回值

node.js - sails.js 嵌套模型

javascript - 如何处理 MongoDB 键名中的点?

node.js - NestJS 拦截器 - 处理 .pipe() 中的错误

node.js - 查询请求时路径的结构是怎样的?

javascript - 不能使用命名空间 'Boom' 作为类型