HTTP requests best practice
See original GitHub issueI am making a number of HTTP requests using the Request module. The responses from these requests need to be unzipped and written to a file in the order the URLs were in.
So far I have the following:
function download(urls, path) {
return new Promise((resolve, reject) => {
const tmpPath = path + '-tmp';
const unzip = zlib.createGunzip();
const output = fs.createWriteStream(tmpPath);
_(urls)
.map(request)
.map(_)
.parallel(5)
.pipe(unzip)
.on('error', (err) => { return reject(err); })
.pipe(output)
.on('finish', () => {
fs.rename(tmpPath, path, (err) => {
if (err) { return reject(err); }
return resolve(path);
});
})
.on('error', (err) => { return reject(err); });
});
}
This works however I have a couple of questions about the implementation.
- Is parallel the best option here? I guess there is a trade off between holding more data in memory or calling the URLs in sequence using
flatMap
. I am after speed so downloading in parallel and using a bit more memory is ok for me. - Is there any way to capture events on the request. If I was using request on its own I would have something like:
request(url)
.on('response', (response) => {
if (response.statusCode === 404) {
return reject(...);
}
})
.on('error', (err) => { return reject(err); });
Is there a way of listening for these events when using Highland?
Any advice greatly received.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9
Top Results From Across the Web
Best practices for REST API design - Stack Overflow Blog
Best practices for REST API design. In this article, we'll look at how to ... This is because our HTTP request method already...
Read more >REST API Best Practices – REST Endpoint Design Examples
In this article, I will take you through 9 best practices to follow while ... HTTP status codes in responses to requests made...
Read more >Best Practices for Designing a Pragmatic RESTful API
The key principles of REST involve separating your API into logical resources. These resources are manipulated using HTTP requests where the ...
Read more >RESTful web API design - Best Practices - Microsoft Learn
REST APIs use a stateless request model. HTTP requests should be independent and may occur in any order, so keeping transient state information ......
Read more >REST API Best Practices — Decouple Long-running Tasks ...
REST API Best Practices — Decouple Long-running Tasks from HTTP Request Processing. Part 1 : Discuss how to design and complete long-running ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for your help @vqvu, here are all the bits combined in case it helps anyone else:
Yep. That’s the trade off. If you’re after download speed, then
parallel
is the right thing to do here. Tune the parallelism factor as necessary.Yes, but it’s a bit more complicated. By default, doing
_(request(url))
will already listen for theerror
event and propagate it correctly. But if you want to listen to multiple custom events, you’ll need to write your own stream generator.Something like this. Note that I’m not calling
reject
until fairly late in the pipeline, and I’m using through instead ofpipe
. It’s generally more convenient to keep everything as a Highland stream for as long as possible, since Highland does nice things like error-propagation for you.In this case, it’s also important to call
push(error)
instead ofreject(error)
ingetUrl
, since otherwise, you will have rejected the promise but not stop the stream.