简体   繁体   English

修改请求主体,然后在Node.js中进行代理

[英]Modify Request body and then proxying in Node.js

I am a relative newbie of Node.js. 我是Node.js的相对新手。 It been two days that I am trying to modify the body of a Request in Node.js and then forwarding it. 我试图修改Node.js中的Request的主体然后转发它已经两天了。 For proxying I am using http-proxy module. 对于代理,我使用的是http-proxy模块。

What I have to do is to intercept the password of a user inside a JSON object, encrypting it and set the new encrypted password inside the request body. 我要做的是拦截JSON对象内的用户密码,加密它并在请求体内设置新的加密密码。

The problem is that every time I try to collect the request body I consume it (ie using body-parser ). 问题是每次我尝试收集请求体时我都会使用它(即使用body-parser )。 How can I accomplish this task? 我怎样才能完成这项任务? I know that the Request in node is seen has a stream. 我知道看到节点中的Request有一个流。

For sake o completeness, I am using express to chain multiple operation before proxying. 为了完整起见,我在代理之前使用express来链接多个操作。

EDIT 编辑

The fact that I have to proxy the request is not useless. 我必须代理请求这一事实并非毫无用处。 It follows the code that I am trying to use. 它遵循我尝试使用的代码。

function encipher(req, res, next){
    var password = req.body.password;
    var encryptionData = Crypto().saltHashPassword(password);
    req.body.password = encryptionData.passwordHash;
    req.body['salt'] = encryptionData.salt;
    next();
}

server.post("/users", bodyParser.json(), encipher, function(req, res) {
    apiProxy.web(req, res, {target: apiUserForwardingUrl});
});

The server (REST made by Spring MVC) give me the exception Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: null 服务器(由Spring MVC制作的REST)给我异常Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: null

The real problem is that there is an integration problem between modules body-parser and http-proxy , as stated in this thread. 真正的问题是模块body-parserhttp-proxy之间存在集成问题, 如此线程中所述。

One solution is to configure body-parser after http-proxy . 一种解决方案是在http-proxy之后配置body-parser If you can't change the order of the middleware (as in my case), you can restream the parsed body before proxying the request. 如果您无法更改中间件的顺序(如我的情况),您可以在代理请求之前重新调整已解析的主体。

// restream parsed body before proxying
proxy.on('proxyReq', function(proxyReq, req, res, options) {
    if (req.body) {
        let bodyData = JSON.stringify(req.body);
        // if content-type is application/x-www-form-urlencoded -> we need to change to application/json
        proxyReq.setHeader('Content-Type','application/json');
        proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
        // stream the content
        proxyReq.write(bodyData);
    }
}

Why don't use express chaining for this ? 为什么不使用快递链? In your first function just do something like this : 在你的第一个函数中,只需执行以下操作:

req.body.password = encrypt(req.body.password); next();

You just have to use a middleware. 你只需要使用中间件。

body-parser is also just a middleware that parses the request bodies and puts it under req.body body-parser也只是一个中间件,它解析请求主体并将其放在req.body

You can do something like this: 你可以这样做:

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

function encryptPassword(req, res, next) {
    req.body.password = encrypt(req.body.password);
    // You can do anything really here and modify the req

    //call next after you are done to pass on to next function

    next();
}

app.use(encryptPassword);

Generally people use middlewares for authentication, role-based access control etc..... 一般人们使用中间件进行身份验证,基于角色的访问控制等.....

You can use middlewares in particular routes also: 您还可以在特定路由中使用中间件:

app.post('/password', encryptPassword, function(req, res) {
     // Here the req.body.password is the encrypted password....

     // You can do other operations related to this endpoint, like store password in database

     return res.status(201).send("Password updated!!");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM