Last active
February 19, 2018 19:59
-
-
Save nickydonna/6f4fe1e33bc62bf8ae7a397d56cbf06b to your computer and use it in GitHub Desktop.
express delay middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const delayMiddleware = (delay) => (req, res, next) => { | |
setTimeout(next, delay) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const {delayMiddleware} = require('./middleware'); | |
const app = express(); | |
const mildDelay = delayMiddleware(200); | |
const bigDelay = delayMiddleware(30000); | |
app.use(mildDelay) // For all request | |
app.post(mildDelay); // For all POSTs | |
app.put('/asdf/*', bigDelay) // For all PUTs a /asdf/* | |
app.get('hello', delayMiddleware(1000), (req, res) => res.json({hello: true}) // For an specific route |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment