I am configuring rate-limit in my node express application. I am fetching rate limit configurations from external API and trying to configure the rate-limit middleware or trying to update middleware after getting config data but not getting success.
In app._router.stack
, I can see my middleware added but express is not triggering/executing it once I call route/service.
I made one below sample so that you can try to reproduce the problem.
const app = express()
app.get('/', (req, res) => {
res.send({msg: 'success'})
})
app.listen(4200, () => {
app.use(function testMiddleware (req, res, next) { // this middleware is not executing
console.log('hello....')
next()
})
console.log(app._router.stack) // above middleware added in stack.
console.log('Server started successfully!, Open this URL http://localhost:4200')
})
In the above example, It is the same case. If I add middleware after the server start then it is not working. and of course, it is working if I added it before the server started but this is not the case that I am trying to achieve.
Note: I want to add it after the server start. Because fetching config from external API and I have background job that fetches config in 1-5 min and if any changes then apply the setting. All things are automated and don't want to restart the server manually.