2016-10-04 20 views
3

私はNode.jsの相対的な初心者です。 Node.js内のリクエストの本文を変更して転送しようとしているのは2日間です。プロキシの場合、私はhttp-proxyモジュールを使用しています。要求の本文を変更してからNode.jsでプロキシする

私がしなければならないことは、JSONオブジェクト内のユーザーのパスワードを傍受し、それを暗号化し、要求本体内に新しい暗号化パスワードを設定することです。

問題は、私がリクエストボディを収集しようとするたびに、それを消費することです(つまり、body-parserを使用)。どのように私はこの仕事を達成することができますか?私は、ノード内のリクエストにストリームがあることを知っています。

私は完全性のために、expressを使用して、プロキシ処理の前に複数の操作を連鎖させています。

EDIT

私はプロキシにリクエストが役に立たないという事実。それは私が使用しようとしているコードに従います。

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

サーバー(RESTは、春のMVC製)を例外に私は、このために急行チェーンを使用していないのはなぜFailed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: null

答えて

5

本当の問題は、​​スレッドに記載されているように、モジュールbody-parserhttp-proxyの間に統合の問題があるということです。

の後にbody-parserを設定することが1つの解決策です。私の場合のようにミドルウェアの順序を変更できない場合は、restream要求をプロキシする前に解析された本体を変更することができます。

// restream parsed body before proxying 
proxy.on('proxyReq', function(proxyReq, req, res, options) { 
    if (req.body) { 
     let bodyData = JSON.stringify(req.body); 
     // incase 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); 
    } 
} 

私はこのf *****問題で2日間失ってしまいました。それが役に立てば幸い!

+0

これは、「送信後にヘッダーを設定できません。」というエラーを表示します。 Cf。 https://github.com/nodejitsu/node-http-proxy/issues/1168およびhttps://github.com/nodejitsu/node-http-proxy/issues/908。 –

0

を与えますか?あなただけのミドルウェアを使用する必要が

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

+0

最初にボディパーサーをチェーンしましたか? Beacuse私はそれが要求を消費することに気づいた。 –

+0

あなたは明示的な設定でbody-parserを使う必要があります: 'app.use(bodyParser.urlencoded({extended:true})); \t app.use(bodyParser.json()); ' –

+0

それはリクエストを消費しませんか?次のミドルウェアで利用できるようになりますか? –

0

:あなたの最初の関数で はちょうどこのような何かを行います。人々は、認証、役割ベースのアクセス制御のためのミドルウェアを使用する一般的

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

body-parserは、あなたがこのような何かを行うことができ、リクエストボディを解析するだけのミドルウェアであるとreq.body

の下にそれを置きます

特定のルートでミドルウェアを使用することもできます。

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!!"); 
}); 
+0

apiProxyを呼び出す前にreq.bodyをログに記録してください。 –

関連する問題