使用 request-promise 和 express 从 api 获取数据
Get data from api using request-promise and express
我正在尝试获取存储在我的 api url 中的数据作为响应。
在路由器内部,我正在使用请求承诺从 url 获取数据。
router.get("/", function(req, res) {
let uri = 'http://localhost:3000/api/v1/donations';
let _include_headers = function(body, response, resolveWithFullResponse) {
return {'headers': response.headers, 'data': body};
};
let options = {
method: 'GET',
uri: uri,
json: true,
transform: _include_headers,
}
return request(options)
.then(function(response) {
console.log(response.headers);
console.log(response.data);
});
});
但我收到此错误 Unhandled rejection RequestError: Error: socket hang up
。
我是新手,不知道这个错误是什么意思?我做错了什么?
帮助将是宝贵的。
当你使用.then
时,你还需要使用.catch
.then
中的代码在一切正常时运行,.catch
中的代码在出现错误时运行。
在您的情况下,发生了错误,但是没有为此运行的代码。您需要做的是:
return request(options)
.then(function(response) {
console.log(response.headers);
console.log(response.data);
})
.catch(function(error){
console.error('An error occured: ' + error);
});
但这只是对错误的优雅处理。您如何处理 "Socet hang up" 错误?这基本上意味着您正在向其发出请求的软件(位于 http://localhost:3000/api/v1/donations 的软件)已接受您的连接,但不会在给定时间(通常为 30 秒)内发送任何响应。问题出在另一个软件上。
我在旅游代码中看到的另一个问题是,您需要在代码中的某处执行 res.send()
或 res.end()
,否则用户将收到错误、空白页或无休止地加载页面。
需要根据请求处理错误:检查 catch 块上的错误。
.catch(error => {
console.error(error)
res.status(403).send({message: error.message})
});
需要重启回复:
res.send(response.data)
完成:
router.get("/", function (req, res) {
let uri = "http://localhost:3000/api/v1/donations";
let _include_headers = function (body, response, resolveWithFullResponse) {
return { headers: response.headers, data: body };
};
let options = {
method: "GET",
uri: uri,
json: true,
transform: _include_headers,
};
request(options).then(function (response) {
res.send(response.data)
}).catch(error => {
res.status(403).send({message: error.message})
});
});
我正在尝试获取存储在我的 api url 中的数据作为响应。
在路由器内部,我正在使用请求承诺从 url 获取数据。
router.get("/", function(req, res) {
let uri = 'http://localhost:3000/api/v1/donations';
let _include_headers = function(body, response, resolveWithFullResponse) {
return {'headers': response.headers, 'data': body};
};
let options = {
method: 'GET',
uri: uri,
json: true,
transform: _include_headers,
}
return request(options)
.then(function(response) {
console.log(response.headers);
console.log(response.data);
});
});
但我收到此错误 Unhandled rejection RequestError: Error: socket hang up
。
我是新手,不知道这个错误是什么意思?我做错了什么?
帮助将是宝贵的。
当你使用.then
时,你还需要使用.catch
.then
中的代码在一切正常时运行,.catch
中的代码在出现错误时运行。
在您的情况下,发生了错误,但是没有为此运行的代码。您需要做的是:
return request(options)
.then(function(response) {
console.log(response.headers);
console.log(response.data);
})
.catch(function(error){
console.error('An error occured: ' + error);
});
但这只是对错误的优雅处理。您如何处理 "Socet hang up" 错误?这基本上意味着您正在向其发出请求的软件(位于 http://localhost:3000/api/v1/donations 的软件)已接受您的连接,但不会在给定时间(通常为 30 秒)内发送任何响应。问题出在另一个软件上。
我在旅游代码中看到的另一个问题是,您需要在代码中的某处执行 res.send()
或 res.end()
,否则用户将收到错误、空白页或无休止地加载页面。
需要根据请求处理错误:检查 catch 块上的错误。
.catch(error => {
console.error(error)
res.status(403).send({message: error.message})
});
需要重启回复:
res.send(response.data)
完成:
router.get("/", function (req, res) {
let uri = "http://localhost:3000/api/v1/donations";
let _include_headers = function (body, response, resolveWithFullResponse) {
return { headers: response.headers, data: body };
};
let options = {
method: "GET",
uri: uri,
json: true,
transform: _include_headers,
};
request(options).then(function (response) {
res.send(response.data)
}).catch(error => {
res.status(403).send({message: error.message})
});
});