我想使用xml2js在Node.js路由中进行多个api调用,然后将每个结果传递给testpage.ejs。
我的以下代码适用于单个api请求,但我不确定如何处理多个:
const requestPromise = require('request-promise'),
xml2js = require('xml2js').parseString,
express = require("express"),
app = express();
const port = 3200,
apiURL = 'https://api.example1.com',
apiURL2 = 'https://api.example2.com';
app.set("view engine", "ejs");
app.use('/public', express.static(__dirname + "/public"));
app.get("/testpage", function(req, res){
requestPromise.post(apiURL, (error, response, body) => {
if(error){
console.log(error);
return error;
}
}).then( (body) => {
xml2js(body, (err, result) => {
if(err){
console.log(err);
} else {
res.render("testpage", {myApiObject: result});
return result;
}
});
});
});
app.listen(process.env.PORT || port, function(){
console.log("Server is running...");
});
下面是我尝试编写一个函数“ callSecondApi()”,将其放置在当前请求的“ else {”语句内,并将结果存储在变量(myApiObject2)中的尝试。不幸的是,这不起作用,因为在尝试将其写入testpage.ejs的控制台时,在myApiObject2上收到“未定义”错误。
function callSecondApi(){
requestPromise.post(apiURL2, (error, response, body) => {
if(error){
console.log(error);
return error;
}
}).then( (body) => {
xml2js(body, (err, result) => {
if(err){
console.log(err);
} else {
myApiObject2 = result;
return result;
}
});
});
}
app.get("/testpage", function(req, res){
var myApiObject2;
requestPromise.post(apiURL, (error, response, body) => {
if(error){
console.log(error);
return error;
}
}).then( (body) => {
xml2js(body, (err, result) => {
if(err){
console.log(err);
} else {
callSecondApi();
res.render("testpage", {myApiObject: result, myApiObject2: myApiObject2});
return result;
}
});
});
});
testpage.ejs代码:
<html>
<head>
<title>
</title>
</head>
<body>
<p>This is the testpage</p>
<script>
var myObj =<%-JSON.stringify(myApiObject)%>
console.log(myObj);
var myObj2 =<%-JSON.stringify(myApiObject2)%>
console.log(myObj2);
</script>
</body>
</html>
任何帮助将不胜感激。
修改:尝试2 。尝试将API请求代码用作Node.js路由内的回调,以检索两个API响应并将它们传递给测试页。这在一定程度上起作用。有时,一切正常,控制台按预期方式打印,其他时候我得到
未捕获到的SyntaxError:意外令牌var
,当我在Chrome中选中“源”时,我会看到:
var myObj =
var myObj2 ={"api_response"}
第一个响应有时似乎不起作用。我不确定是什么原因造成的。
下面的代码更改:
const requestPromise = require('request-promise'),
xml2js = require('xml2js').parseString,
express = require("express"),
app = express();
const port = 3200,
apiURL = 'https://api.example1.com',
apiURL2 = 'https://api.example2.com';
app.set("view engine", "ejs");
app.use('/public', express.static(__dirname + "/public"));
app.get("/testpage", function(req, res){
var myApiObj1;
var myApiObj2;
// Main function to make API calls
function firstApiCall(callback){
requestPromise.post(apiURL, (error, response, body) => {
if(error){
console.log(error);
return error;
}
}).then( (body) => {
xml2js(body, (err, result) => {
if(err){
console.log(err);
} else {
myApiObj1 = result;
return result;
}
});
});
callback();
}
// Callback function
function secondApiCall(){
requestPromise.post(apiURL2, (error, response, body) => {
if(error){
console.log(error);
return error;
}
}).then( (body) => {
xml2js(body, (err, result) => {
if(err){
console.log(err);
} else {
myApiObj2 = result;
res.render("testpage", {myApiObject: myApiObj1, myApiObject2: myApiObj2});
return result;
}
});
});
}
firstApiCall(function() {
secondApiCall();
});
});
app.listen(process.env.PORT || port, function(){
console.log("Server is running...");
});