在 express JS 服务器中使用 http-proxy

迪纳斯塔66

我在 web 上获得了一个基于 express nodeJS 的登录页面代码。

我有许多后端应用程序在不同的端口上运行。目标是,当用户在 nodeJS 服务器中进行身份验证时,他会自动重定向到他的应用程序。

但是,如果我可以安装一个分离的 http-proxy 并正确运行它,我希望在用户连接时在此登录 JS 代码中包含代理。

下面是代码的一部分。

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);

response.redirect('/home');我想通过代理替换它,但没有附加任何内容。

我不知道这是否可能,因为在同一个实例上运行 2 个服务器。

谢谢您的帮助。

看待

阿兰德塞纳

我认为如果你使用 express 最好使用这个包:https ://www.npmjs.com/package/express-http-proxy

你可以像这样使用它:

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 );

然后,当您登录时,您会重定向到您代理的路线:

res.redirect('/youRoute/');

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

基于Node.js的服务器与类似Apache HTTP服务器的服务器

具有http.createServer的服务器与在节点js中使用express的服务器之间的区别

node.js服务器和带有express.js的HTTP / 2(2.0)

如何在express.js中使用服务器发送事件

使用Express http-proxy从API存储令牌

如何使用Express关闭Node.js中的HTTP连接

启动node.js Express服务器即服务

如何使用Express-http-proxy使用Node代理以剥离最终的剩余部分

setInterval是否会阻止/干扰node.js / express HTTP服务器?

关于HTTP服务器与在NodeJS中使用http的混淆

使用http-proxy-middleware时,表单参数未传递到Express应用程序

我应该在无服务器应用程序中使用Express.js吗?

如何在Elastic Beanstalk AWS上使用MySQL托管节点js / express服务器

如何创建与Express或http反应的服务器?

如何使用socket.io从Express Node.js服务器发送数据

可以使用express在node.js中以相同的端口号运行http和https服务器吗?

不使用express.js将图像保存到Node.js服务器

Node.js HTTP服务器

在Node.JS和Express中的HTTPS服务器上拦截HTTP响应

为什么需要将Express服务器实例作为参数传递给Node.JS中的http模块?

Express JS委托HTTP动词

使用Sails和Express在localhost服务器之间创建HTTP请求

无法在Android中使用OkHttp3将JSON发布到服务器Express js

如何使用express-http-proxy修改响应头

SocketIO服务器,Express和http模块

使用 React.js 和 Express.js POST 表单数据到服务器

使用 node js express 服务器调用 python 脚本

使用 express js 的 http-auth 摘要

使用 PhpStorm 在 node.js Express 中调试 HTTP 请求