Skip to content

Instantly share code, notes, and snippets.

@cb-v4s
Last active July 16, 2021 21:20
Show Gist options
  • Select an option

  • Save cb-v4s/042652c591ce8fc75a8b3894f0af8ab3 to your computer and use it in GitHub Desktop.

Select an option

Save cb-v4s/042652c591ce8fc75a8b3894f0af8ab3 to your computer and use it in GitHub Desktop.
express webserver rate limiting and more security measures
const limitter = require('express-rate-limit')
const helmet = require('helmet')
...
// middlewares
app.use(
limitter({
windowMs: 5000,
max: 5,
message: {
code: 429, /* Too Many Requests */
message: "You've made too many requests recently. Please wait and try your request again later."
}
})
)
app.use(express.csrf()); // csrf protection
app.use(helmet.xframe())
app.use(helmet.iexss())
app.use(helmet.contentTypeOptions())
app.use(helmet.cacheControl())
app.use((req, res, next) => {
delete req.headers['x-powered-by'];
next();
});
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment