What is timed-out?
The 'timed-out' npm package is used to timeout HTTP requests. It provides a simple way to set timeouts for various stages of an HTTP request, such as the connection, socket, and response stages.
What are timed-out's main functionalities?
Set timeout for HTTP request
This feature allows you to set a timeout for an HTTP request. In this example, a timeout of 5 seconds is set for the HTTP GET request to 'http://example.com'. If the request takes longer than 5 seconds, it will be aborted.
const http = require('http');
const timedOut = require('timed-out');
const req = http.get('http://example.com', (res) => {
// Handle response
});
// Set a timeout of 5 seconds
const timeout = 5000;
timedOut(req, timeout);
Set timeout for specific stages
This feature allows you to set timeouts for specific stages of an HTTP request. In this example, different timeouts are set for the connection, socket, and response stages of the HTTP GET request to 'http://example.com'.
const http = require('http');
const timedOut = require('timed-out');
const req = http.get('http://example.com', (res) => {
// Handle response
});
// Set timeouts for different stages
const options = {
connect: 1000, // 1 second for connection
socket: 2000, // 2 seconds for socket
response: 3000 // 3 seconds for response
};
timedOut(req, options);
Other packages similar to timed-out
axios
Axios is a promise-based HTTP client for the browser and Node.js. It allows you to set timeouts for requests and provides a more comprehensive set of features for making HTTP requests compared to 'timed-out'.
request
Request is a simplified HTTP client that supports timeouts and many other features. It is more feature-rich than 'timed-out' and provides a higher-level API for making HTTP requests.
got
Got is a human-friendly and powerful HTTP request library for Node.js. It supports timeouts and provides a more modern and flexible API compared to 'timed-out'.
timed-out
Timeout HTTP/HTTPS requests
Emit Error object with code
property equal ETIMEDOUT
or ESOCKETTIMEDOUT
when ClientRequest is hanged.
Usage
var get = require('http').get;
var timeout = require('timed-out');
var req = get('http://www.google.ru');
timeout(req, 2000);
API
timedout(request, time)
request
Required
Type: ClientRequest
The request to watch on.
time
Required
Type: number
or object
Time in milliseconds to wait for connect
event on socket and also time to wait on inactive socket.
Or you can pass Object with following fields:
connnect
- time to wait for connectionsocket
- time to wait for activity on socket
License
MIT © Vsevolod Strukchinsky