我正在使用jquery向'/ get_access_token'url发出发布请求。但它没有通过。在邮递员中,请求被卡在“发送”中。在下面添加代码。
app.js
const myRouter = require('./src/routes/myRouter ');
app.use('/get_access_token', myRouter );
myRouter,js
//this is inside myRouter
const express = require('express');
const myRouter= express.Router();
const myController = require('../controllers/myController');
function router() {
debug('inside router');
const { getAccessToken } = myController();
debug(getAccessToken);
//from here it is not going down
myRouter.route('/get_access_token').post((request) => {
debug('inside api call');
const result = getAccessToken(request);
});
}
module.exports = router;
myController.js
const client = require('path to client');
function myController() {
function getAccessToken(request) {
debug('inside getAccessToken');
const PUBLIC_TOKEN = request.body.public_token;
Promise((resolve, reject) => {
client.exchangePublicToken(PUBLIC_TOKEN, (error, tokenResponse) => {
if (error != null) {
reject(new Error('Could not get access token'));
}
const ACCESS_TOKEN = tokenResponse.access_token;
const ITEM_ID = tokenResponse.item_id;
debug(ACCESS_TOKEN);
debug(ITEM_ID);
resolve('Access Token was acquired succesfully');
});
});
}
return {
getAccessToken
};
}
module.exports = myController;
jquery请求,只有存在公共令牌的部分
onSuccess: function(public_token) {
debugger;
$.post('/get_access_token', {
public_token: public_token
}, function() {
$('#container').fadeOut('fast', function() {
$('#intro').hide();
$('#app, #steps').fadeIn('slow');
});
});
}
答案 0 :(得分:0)
这是问题
module.exports = router;
路由器是功能,实际上不是路由器,它无法工作。
如果您导出myRouter
,从理论上讲它可以工作,但是必须正确定义它。
我建议在互联网上找到一些“表达样板”,使其工作,然后根据您的需要更新该代码