Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda