Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
How to forward POST request to another URL and render response in Nuxt(SSR) serverMiddleware

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

  1. GET /some-request - should just render the page (that's working fine)
  2. 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.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda