2017-10-13 25 views
0

expressJSでリクエストとレスポンスに関する質問が1つあります。私のケースでは、リクエストをサーバーに送信し、JSONでBearerキーを取得しますが、このキーはセッションごとに異なります。注文を作成すると2番目のリクエストがありますが、トランザクションを承認するにはこのベアラーキーが必要です。そして、私の質問は、ある要求から別の要求にデータを送ることができますか?ベアラ番号'Authorization'フィールドに挿入する必要があります。私のコードを見てください。あなたは、一般的に最初に戻って、クライアントに行くあなたのトークンを、作成されたかどう1つのリクエストから別のリクエストにJSON応答を送信する

router.post('/authorize', function(req, res){ 
request({ 
    method: 'POST', 
    url: 'https://secure.snd.payu.com/pl/standard/user/oauth/authorize', 
    headers: { 
     'Content-Type': 'application/x-www-form-urlencoded', 
    }, 
    body: "xyz" 
}, function (error, response, body) { 
     console.log('Status:', response.statusCode); 
     console.log('Headers:', JSON.stringify(response.headers)); 
     console.log('Response:', body); 
     res.send(body); //Here I get necessary Bearer key 
    } 
)} 


router.post('/paynow', function(req, res){ 
    request({ 
method: 'GET', 
url: 'https://secure.snd.payu.com/api/v2_1/paymethods/', 
headers: { 
    'Authorization': 'Bearer number' 
}}, function (error, response, body) { 
     console.log('Status:', response.statusCode); 
     console.log('Headers:', JSON.stringify(response.headers)); 
     console.log('Response:', body); 
     res.send(body); 
} 

) }

+1

通常、最初のリクエストでクライアントにトークンを送信すると、クライアントはこのトークンを「Authorization」ヘッダーに追加して、すべての要求に送信する必要があります。リクエストから別のリクエストに渡す必要はありません この図をhttp://jwt.ioから確認してください: https://cdn.auth0.com/content/jwt/jwt-diagram.png – mJehanno

+0

大丈夫です、ありがとうございました。 –

答えて

0

。クライアントにトークンが追加されました。一般に、これはコード化され、ユーザー名、ID、特殊な役割、期限切れ時間などの情報を含みます。

その後、そのトークンを後続の要求で付与します。

あなたのコードの構文に基づいて、私はあなたがexpress.jsを使用していると仮定しています。私がここで間違っているなら、私を修正してください。とにかく、それは "ミドルウェア"として知られている概念を使用しています。基本的には、応答を返す前に他のjavascript関数を実行することができます...あなたはこのようなものが必要です。それはおそらく動作しないように、私は実際に出てこのコードをテストしていないことを知っているが、うまくいけば、それは正しい方向にあなたを指す:

router.post('/authorize', function(req, res) { 
    request({ 
     method: 'POST', 
     url: 'https://secure.snd.payu.com/pl/standard/user/oauth/authorize', 
     headers: { 
      'Content-Type': 'application/x-www-form-urlencoded', 
     }, 
     body: "xyz" 
    }, function(error, response, body) { 
     console.log('Status:', response.statusCode); 
     console.log('Headers:', JSON.stringify(response.headers)); 
     console.log('Response:', body); 
     res.send(body); //Here I get necessary Bearer key 
    }); 
}); 


router.post('/paynow', decodeToken, function(req, res) { 
    // here you could now access the user information on the user property of the request. It's assumed that at this point, the token has all been validated 
    const user = req.user; 
    console.log(user); 
    res.send(`Hello, ${user.name}!`); 
}); 

const decodeToken = (req, res, next) => { 
/* do something to decode the token. Review the documentation for whichever JWT library your'e using on how to do this. It will be something like this. */ 
jwt.decode(req.header['Authorization']) 
    .then((decodedToken) => { 
     req.user = decodedToken; 

     // notice this next() function. this tells express to execute the next function in line on the router endpoint you have. 
     next(); 
    }) 
    .catch((error) => { 
     // error usually something along the lines of an expired token, token doesn't exist, invalid, etc. 
     console.log(error); 

     // we immediately send a bad response back. Thus, code would never even get into the main '/paynow' logic. 
     res.status(500).end(); 
    }); 
}); 

は、ミドルウェアとしてdecodeToken機能を使用すると、どのようにそれを注意してくださいnext()の呼び出し。 Expressミドルウェア、またはこのような状況をどのように処理するかについて使用しているライブラリを見直すことをお勧めします。

また、サイドノートでは、あなたが投稿したコードに基づいて "Call Back hell"と呼ばれるものに向かって忍び寄っています。それはあなたの質問とは関係ありませんが、私はあなたにGoogleの "コールバック地獄"が悪い理由と、あなたがそれを使用しないように教えるために何ができるのかを見ることをお勧めします。 :)ハッピーコーディング!

関連する問題