如何发送 response.body 请求模块功能?
How can I send the response.body out of request module function?
我首先在请求函数中创建一个 return
语句(我已经链接了一张图片),然后 console.log
它在 function
之外,但是没有成功.
我的服务器代码
var options = {
'method': 'POST',
'url': 'http://localhost:8080/ES_Part1/api/user/getUser',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'username': username,
'password': password
}
};
requestToApi(options, function(error, response) {
if (error) throw new Error(error);
console.log("Send form data to remote api and to return the user from Spring")
console.log(response.body);
return response.body
});
var fromapi = response.body;
res.end();
示例:
我建议您在这里使用基于 Promise
的方法,而不是您为 requestToApi
使用的回调方式。如果您使用的是 request
软件包,则可以使用基于 Promise
的版本。
另一种解决方案是自己创建一个 promise
,例如:
var requestToApiAsPromise = (options) => {
return new Promise((resolve, reject) => {
requestToApi(options, (error, response) => {
if (error) {
reject(error)
return
}
resolve(response.body)
})
})
}
那么你可以在你的中间件中使用这个方法:
app.post("/checkUser", (req, res) => {
async function process() {
try {
var username = req.body.username
var password = req.body.password
var options = {...}
var response = await requestToApiAsPromise(options)
// response => response.body
// do whatever
res.end()
} catch (error) {
next(error)
}
}
process()
})
此方法使用 async/await
,这样您就可以像同步执行操作一样编写代码,因此可以更轻松地进行异步调用并将它们 "wait" 放在下一行之前被处决。
如果你想在处理程序之外获得 respose.body
,你可以使用下面的代码:
// an example get function
app.get('/users', async(req, res) => {
var options = {
'method': 'POST',
'url': 'http://localhost:8080/ES_Part1/api/user/getUser',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'username': username,
'password': password
}
};
const result = new Promise((resolve, reject) => {
requestToApi(options, function(error, response) {
if (error) return reject(error);
return resolve(JSON.parse(response.body));
});
})
// make sure, to use async in your function
// because we're using await here
var fromapi = await result;
// It's working here
console.log(fromapi);
res.end();
})
上面的代码,只是一个例子,你可以用来阅读response.body
。如果你想处理上面代码的错误,你可以像下面这样使用代码:
try {
// make sure, to use async in your function
// because we're using await here
var fromapi = await result;
// It's working here
console.log(fromapi);
} catch(ex) {
// print error response
console.log(ex.message);
}
希望对您有所帮助。
我首先在请求函数中创建一个 return
语句(我已经链接了一张图片),然后 console.log
它在 function
之外,但是没有成功.
我的服务器代码
var options = {
'method': 'POST',
'url': 'http://localhost:8080/ES_Part1/api/user/getUser',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'username': username,
'password': password
}
};
requestToApi(options, function(error, response) {
if (error) throw new Error(error);
console.log("Send form data to remote api and to return the user from Spring")
console.log(response.body);
return response.body
});
var fromapi = response.body;
res.end();
示例:
我建议您在这里使用基于 Promise
的方法,而不是您为 requestToApi
使用的回调方式。如果您使用的是 request
软件包,则可以使用基于 Promise
的版本。
另一种解决方案是自己创建一个 promise
,例如:
var requestToApiAsPromise = (options) => {
return new Promise((resolve, reject) => {
requestToApi(options, (error, response) => {
if (error) {
reject(error)
return
}
resolve(response.body)
})
})
}
那么你可以在你的中间件中使用这个方法:
app.post("/checkUser", (req, res) => {
async function process() {
try {
var username = req.body.username
var password = req.body.password
var options = {...}
var response = await requestToApiAsPromise(options)
// response => response.body
// do whatever
res.end()
} catch (error) {
next(error)
}
}
process()
})
此方法使用 async/await
,这样您就可以像同步执行操作一样编写代码,因此可以更轻松地进行异步调用并将它们 "wait" 放在下一行之前被处决。
如果你想在处理程序之外获得 respose.body
,你可以使用下面的代码:
// an example get function
app.get('/users', async(req, res) => {
var options = {
'method': 'POST',
'url': 'http://localhost:8080/ES_Part1/api/user/getUser',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'username': username,
'password': password
}
};
const result = new Promise((resolve, reject) => {
requestToApi(options, function(error, response) {
if (error) return reject(error);
return resolve(JSON.parse(response.body));
});
})
// make sure, to use async in your function
// because we're using await here
var fromapi = await result;
// It's working here
console.log(fromapi);
res.end();
})
上面的代码,只是一个例子,你可以用来阅读response.body
。如果你想处理上面代码的错误,你可以像下面这样使用代码:
try {
// make sure, to use async in your function
// because we're using await here
var fromapi = await result;
// It's working here
console.log(fromapi);
} catch(ex) {
// print error response
console.log(ex.message);
}
希望对您有所帮助。