是否可以使用 nginx 合并两个 json 响应? [英] Is it possible to merge two json responses using nginx?
问题描述
我有一个现有的快速端点,如下所示:
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 执行您的网络服务的逻辑"会更好,因为 nginx 即将为您的网站/网络服务提供服务并实现逻辑.
"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
语句将如下所示:
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屋!