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

218
Vistas
use http-proxy inside a express JS server

I have get a login page code on the web based on express nodeJS.

I have many backend app running on different port. The goal is, when a user is authentificated in the nodeJS server, he's automaticaly redirected to his app.

But, if i can mount a http-proxy separated and run it properly, i would want to include proxying in this loggin JS code when user is connected.

There is the part of the code below.

const httpProxy = require('http-proxy');
const proxy = httpProxy.createProxyServer();

(...)

// http://localhost:3000/auth
app.post('/auth', function(request, response) {
    // Capture the input fields
    let username = request.body.username;
    let password = request.body.password;
    // Ensure the input fields exists and are not empty
    if (username && password) {
        // Execute SQL query that'll select the account from the database based on the specified username and password
        connection.query('SELECT * FROM accounts WHERE username = ? AND password = ?', [username, password], function(error, results, fields) {
            // If there is an issue with the query, output the error
            if (error) throw error;
            // If the account exists
            if (results.length > 0) {
                // Authenticate the user
                request.session.loggedin = true;
                request.session.username = username;
                // Redirect to home page

                proxy.web(req, res, { target: 'http://127.0.0.1:1881' });

                //response.redirect('/home');
            } else {
                response.send('Incorrect Username and/or Password!');
            }           
            response.end();
        });
    } else {
        response.send('Please enter Username and Password!');
        response.end();
    }
});
app.listen(3000);

At the response.redirect('/home');i want to replace it by proxying, but nothing append.

I don't know if this is possible, because running 2 servers on the same instance.

Thank you for your help.

Regard

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think it is better to use this package if you use express: https://www.npmjs.com/package/express-http-proxy

You can use it like this:

const { createProxyMiddleware, fixRequestBody, responseInterceptor } = require( 'http-proxy-middleware' );

const proxyMiddleware = createProxyMiddleware({
  target: 'foo',
  onProxyReq: fixRequestBody,
  logLevel: 'debug',
  changeOrigin: true,
  secure: false,
  xfwd: true,
  ws: true,
  hostRewrite: true,
  cookieDomainRewrite: true,
  headers: {
    "Connection": "keep-alive",
    "Content-Type": "text/xml;charset=UTF-8",
    "Accept": "*/*"
  },
});

app.use( '/youRoute/**', proxyMiddleware );

And then when you are loged in you redirect to you you proxified route :

res.redirect('/youRoute/');
about 4 years ago · Juan Pablo Isaza Denunciar
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