• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

128
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 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda