是否可以使用nginx合并两个json响应? [英] Is it possible to merge two json responses using nginx?

查看:74
本文介绍了是否可以使用nginx合并两个json响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的快速端点,如下所示:

I have an existing express endpoint that looks like this:

app.get(`${route}/:id`, async (req, res) => {
    try {
        const id = req.params.id;
        const result = await dbFn(id);
        res.send(result);
    } catch (err) {
        res.status(500).end();
    }
});

这将返回一个看起来像这样的对象:

And this is going to return an object that looks like:

{
    "id": 123,
    "name": "Foo"
}

现在,我想扩展此API,以便如果它具有 Accept:application/vnd.v2 标头,那么它还将从其他服务中获取一些数据,并将其添加到.(在哪里看到我的相关问题建议使用内容协商).

Now, I want to extend this API, so that if it has a Accept: application/vnd.v2 header, then it will also fetch some data from a different service, and add that on. (See my related question where using content negotiation is suggested).

即.响应将是:

{
    "id": 123,
    "name": "Foo", 
    "extraData": {
        "foo": "bar"
    }
}

现在,我可以使用express做到这一点,这是我的做法:

Now, I can do this with express, here's how I have done it:

  app.get(`${route}/:id`, async (req, res, next) => {
    try {

      const id = req.params.id;
      const jobSeeker = await dbFn(id);
      if (req.accepts("application/vnd.v2")) {
        const response = await axios.get(`${integrationApiPath}/connection/${id}`); 
        const ssiData = response.data; 

        res.send({
          ...jobSeeker, 
          ssiData
        })

      }
      else {
        res.send(jobSeeker);
      }
    } catch (err) {
      res.status(500).end();
    }

  });

但这让我感到有些困惑,因为它进行API版本控制.

But it struck me as a bit of a messy way to do API versioning.

如果我可以让Nginx来处理此版本控制,那就更好了.

What would be much nicer, is if I can have nginx handling this versioning instead.

那样,我不需要修改现有的API,只需创建新服务,让nginx检查标头,然后进行两个微服务调用并将它们连接在一起.

That way, I don't need to modify my existing API, I can just create the new service, and have nginx examine the headers, and make both microservice calls and join them together.

这可能吗?

推荐答案

但这让我感到有些困惑,因为它进行API版本控制."我认为这不是进行API版本控制的坏方法,因为这是常用的方法.另外,您可以在新的子目录(例如yourwebsite.com/yourservice.../v2/yourFunction)中提供新服务.

"But it struck me as a bit of a messy way to do API versioning." I don't think that this is a bad way to do API versioning, since it's the comman way to do it. I addition you can serve the new service in a new subdirectory (e.g. yourwebsite.com/yourservice.../v2/yourFunction).

如果我可以让Nginx代替此版本控制,那就更好了."我也不会确认让nginx做您的web服务的逻辑"会更好,因为nginx即将为您的网站/webservice提供服务并实现该逻辑.

"What would be much nicer, is if I can have nginx handling this versioning instead." I also won't confirm that it would be nicer to let nginx do the "logic" of your webservice, since nginx is about to serve your website/webservice and to to implement the logic.

但是,如果您仍然想使用nginx合并请求,则可能需要查看 openresty .您可能必须先安装此.

However, if you still want to merge the requests using nginx you may want to have a look at this question/answer. This answer uses openresty. You may have to install this first.

如前所述,您可以使用以下代码调用多个(在您的情况下为2)服务:

As described, you can call multiple (in your case 2) services using this code:

location /yourServiceV2 {
    content_by_lua_block {
        local respA = ngx.location.capture("/yourService")
        local respB = ngx.location.capture("/theServiceWhichExtendsYourService")

        ngx.say(respA.body .. respB.body)
    }
}

如果您只想在出现特定标头时在下面执行提到的代码,则可以使用 if 语句,如

If you only want to perform the mentioned code under when a specific header is present you can use a if statement like described in this answer. So, your if statement would look like this:

if ($http_accept == 'application/vnd.v2') {
    return 405;
}

这篇关于是否可以使用nginx合并两个json响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆