如何根据路径参数有条件地调用代理服务器?

时间:2016-08-12 00:52:43

标签: hapijs

let post = 
{
  method: 'POST',
  path: '/posts/new/{postType}',
  handler: (request, reply) =>{
       //when params is text
  if (request.params.postType == "text") 
  {
    return reply("successfully uploaded text post");
  }
   //when params is image
  else (request.params.postType == "image") 
  {
    //call proxy uri to save the image
    /*proxy: 
     {
         uri: proxyUri + '/post/image', //call proxy uri to save the image
         passThrough: true,
         acceptEncoding: false,
      }*/
   }
 }

如何基于路由参数有条件地调用代理服务器,因此我使用一个休息api来发布文本,图像,视频等?

1 个答案:

答案 0 :(得分:1)

您可以使用reply.proxy()。

const route = {
    /* ... */
    handler: (request, reply) => {
        if (request.params.useproxy) {
            return reply.proxy({host: 'example.com', port: 80, protocol: 'http'});
        } else {
            return reply('no proxy');
        }
    }
};