【发布时间】:2020-08-24 23:52:03
【问题描述】:
我正在尝试建立一个小型网站。我使用 React 作为前端,Nodejs 作为后端,以及一些第三方 API。这里我的想法是,首先将表单数据发布到nodejs。从那时起,我接受节点中的数据并需要调用外部 api。为此,我正在使用 axios。从我的 api 接收值后,我必须将该值发送回响应应用程序。当我在邮递员中运行我的代码时,输出是 {}。我认为我没有从我的 api 中获取值,但不知道如何解决这个问题。我对这些技术很陌生。有人请帮我解决这个问题。提前谢谢你。这是我到目前为止所尝试的。
const express = require('express');
const axios = require('axios');
const router = express.Router();
const request = require('request');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended : false}));
router.get('/', (req, res) => {
res.send(" Express Homepage is running...");
});
async function callApi(emailid, pswd) {
return axios({
method:'post',
url:'http://51.X.X/api/login',
data: {
"email": `${emailid}`,
"password": `${pswd}`
},
headers: {'Content-Type': 'application/json' }
})};
callApi().then(function(response){
return response.data;
})
.catch(function(error){
console.log(error);
})
app.post('/api/login', (req, res) => {
let emailid = String(req.body.email);
let pswd = String(req.body.password);
const data = callApi(emailid, pswd);
if(data) {
res.send(data);
}else {
res.json({msg : " Response data not recieved.."})
}
});
【问题讨论】: