2017-10-13 190 views
0

我在expressJS中提出了一个关于请求和响应的问题。我在一个请求中发送请求到服务器,并在JSON中获取承载密钥,但是此密钥在每个会话中都不相同。当我创建订单时,我有第二个请求,但我需要此持票人密钥来授权交易。我的问题是从一个请求发送数据到另一个?不记名号码我必须插入'Authorization'字段。请看我的代码。从一个请求发送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()上调用。我鼓励您查看快递中间件,或者您正在使用的任何库来处理这种情况。

此外,在一个侧面说明...你匍匐向一些所谓的根据你的代码贴有“回叫地狱”。虽然它与你的问题没有任何关系,但我鼓励你谷歌“回头看看”,看看它为什么不好,你可以怎样教你自己不要使用它。 :)快乐的编码!

相关问题