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"
}
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).
ie. the response will be:
{
"id": 123,
"name": "Foo",
"extraData": {
"foo": "bar"
}
}
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();
}
});
But it struck me as a bit of a messy way to do API versioning.
What would be much nicer, is if I can have nginx handling this versioning instead.
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.
Is this possible?