What is connect-pause?
The connect-pause npm package is a middleware for Connect/Express that allows you to introduce a delay in the processing of HTTP requests. This can be useful for simulating network latency or testing how your application behaves under slow network conditions.
What are connect-pause's main functionalities?
Introduce a fixed delay
This feature allows you to introduce a fixed delay (in milliseconds) for all incoming HTTP requests. In this example, a 2-second delay is added to all requests.
const express = require('express');
const pause = require('connect-pause');
const app = express();
// Introduce a 2-second delay for all requests
app.use(pause(2000));
app.get('/', (req, res) => {
res.send('Hello, world!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Conditional delay
This feature allows you to introduce a delay conditionally, for example, only for specific routes. In this example, a 3-second delay is added only to requests to the '/slow' route.
const express = require('express');
const pause = require('connect-pause');
const app = express();
// Introduce a delay only for specific routes
app.use('/slow', pause(3000));
app.get('/slow', (req, res) => {
res.send('This response is delayed by 3 seconds');
});
app.get('/fast', (req, res) => {
res.send('This response is fast');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Other packages similar to connect-pause
express-delay
The express-delay package provides similar functionality to connect-pause by allowing you to introduce delays in your Express application. It offers more granular control over the delay, such as random delays within a specified range. Compared to connect-pause, express-delay provides more flexibility in simulating various network conditions.
express-slow-down
The express-slow-down package is designed to slow down responses based on request rate. It is useful for rate limiting and preventing abuse by introducing delays after a certain number of requests. While connect-pause is more focused on introducing fixed delays, express-slow-down is more dynamic and adjusts delays based on request patterns.
Connect Pause
Connect/Express middleware to simulate latency for debugging
Installation
npm install connect-pause
Usage
Pause all Connect requests:
var connect = require('connect'),
pause = require('connect-pause');
Connect.createServer(
pause(1000),
function(req, res, next){
res.writeHead(200, {'Content-Type':'text/plain'});
res.end('Waited 1 second');
}
).listen(8080);
Pause all Express requests:
var express = require('express'),
pause = require('connect-pause');
var app = express();
app.use(pause(1000));
app.get('/', function(req, res){
res.send('Waited 1 second');
});
app.listen(3000);
Pause a single Express endpoint:
var express = require('express'),
pause = require('connect-pause');
var app = express();
app.get('/', pause(1000), function(req, res){
res.send('Waited 1 second');
});
app.listen(3000);
Pass an error to be returned after the delay:
var express = require('express'),
pause = require('connect-pause');
var app = express();
app.get('/', pause(1000, new Error('Send 500')), function(req, res){
res.send('Waited 1 second');
});
app.listen(3000);
When to use
This middleware is meant to be used while developing and in need to simulate latency for certain requests or all.
License
(The MIT License)
Copyright (c) 2013 Ariel Flesler
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.