Open
Description
Hi, I am planning to remove the old dependency request
, since it is deprecated for 2 years, and I prefer to use node-fetch
to instead of it.
Here was an discussion about what libs can alternative with
request
: Alternative libraries to request #3143
There are two things confused me:
- What type and value may
proxy
param is? Is it a URL string? - How to handle error
// server.js
request.get(
{
url: remoteUrl.toString(),
headers: filterHeaders(req, req.headers),
encoding: null,
proxy: proxy,
},
//eslint-disable-next-line no-unused-vars
function (error, response, body) {
let code = 500;
if (response) {
code = response.statusCode;
res.header(filterHeaders(req, response.headers));
}
res.status(code).send(body);
}
);
Will be:
import fetch from "node-fetch";
// ...
let statusCode = 500;
fetch(remoteUrl.toString(), {
headers: filterHeaders(req, req.headers),
agent: proxy // I do not know whether correct or not to set proxy here
}).then((responseStream) => {
statusCode = responseStream.status;
res.header(filterHeaders(req, responseStream.headers));
return responseStream.arrayBuffer(); // use `arrayBuffer()` to pipe responseBody in raw bytes format
}).then((responseBody) => {
res.status(statusCode).send(responseBody);
}).catch((err) => {
// How to handle error?
})
Thanks!