what is the best way to forward a POST request to another URL(in Nuxt SSR) ? for example
GET /some-request - should render page
POST /some-request - should be forwarded to /api/some-request
More examples
GET /some-request
- should just render the page (that's working fine)GET and IF XMLHttpRequest
- if ajax request, forward it to /api/some-request
this also working fine with this trick asyncData({res, ssrContext})
{
if (req.headers['x-requested-with'] === 'XMLHttpRequest') {
redirect({res, ssrContext}, route.fullPath.replace('/some-request', '/api/some-request')
);
return;
}
}
function redirect(ctx, url) {
ctx.ssrContext.redirected = {};
ctx.res.writeHead(302, {
Location: url,
});
ctx.res.end();
}
This works fine with GET request
I need the exact same thing with the POST request.
I thought I can use serverMiddleware
export default (req, res, next) => {
if ( req.headers['x-requested-with'] === 'XMLHttpRequest') {
req.originalUrl = '/api/some-request'
}
next()
}
This does not work, but I am not sure what is the best way to do it. And one more thing. POST request can return either JSON or Render HTML.