在中间件中发出一个Post请求

我是Express JS和Node JS的新手。

我打算在Express Middleware中手动实现Auth-Server,我使用https://github.com/ranm8/requestify发出请求

const requestify = require('requestify'); app.use(function (req, res, next) { var config = require('./config-' + process.env.REACT_APP_ENV) var redirect = config.ADMIN_UI_URL // Perform login to Auth-Server if (req.originalUrl.startsWith('/?code')) { let auth_code = req.query.code let params = { 'grant_type': 'authorization_code', 'code': auth_code, 'client_id': config.OAUTH_CLIENT_ID, 'redirect_uri': redirect } requestify.post(config.OAUTH_ID_URL+'/oauth2/token', params).then(function(response) { console.log(response.getBody()); // There is not data post to the Auth-Server }); return res.redirect('http://google.com'); } // Check Token if (req.cookies._token === undefined) { let url = config.OAUTH_ID_URL + '/oauth2/authorize?' + 'response_type=code' + '&' + 'client_id=' + config.OAUTH_CLIENT_ID + '&' + 'redirect_uri=' + redirect return res.redirect(url) } next() }) 

我可以检查用户令牌,并收到auth_code就好了。 但是我不能通过向Auth-User发送一个post请求来从login用户请求一个token

好像我很想知道NodeJS,Epxress是如何工作的,或者是在这里的某种范围

请帮帮我! 谢谢

尝试检查你得到的错误是什么。 像这样添加error handling程序

 requestify.post(config.OAUTH_ID_URL+'/oauth2/token', params).then(function(response) { console.log(response.getBody()); // There is not data post to the Auth-Server }).error(function(error){ console.log(error) // see if anything is coming in error object }); 

我的坏,我应该使用https://www.npmjs.com/package/request#forms包

 request.post({url:config.OAUTH_ID_URL+'/oauth2/token', form: bodyData}, function(err,httpResponse,body){ console.log(err); console.log(body); }) 

它现在的作品! 谢谢