alykoshin / express-queue

Express middleware to limit a number of simultaneously processing requests using queue
MIT License
45 stars 12 forks source link

app.use conditionally #1

Open kivervinicius opened 8 years ago

kivervinicius commented 8 years ago

Hello, tks for solution, i need use this section app.use(queue({ activeLimit: 1 })); conditionally, its possible?

alykoshin commented 8 years ago

Hello,

tks for solution

You are welcome.

i need use this section app.use(queue({ activeLimit: 1 })); conditionally, its possible?

Can you provide a bit more details?

kivervinicius commented 8 years ago

I need only be limited to a route, for example /GetProducts other routes need to be unlimited if I leave app.use directly will limit all right?

alykoshin commented 8 years ago

Middleware may be assigned to single route, so you need just following: app.use('/GetProducts', queue({ activeLimit: 1 }) );

kivervinicius commented 8 years ago

Oh tks, i will test.

DalderupMaurice commented 6 years ago

The queue'ing does not seem to work when implementing it like this:

chain.routes.js
import expressQueueMw from 'express-queue';
import chainCtrl from './chain.controller';

const router = Router(); // eslint-disable-line new-cap
router.route('/init').get(chainCtrl.init, expressQueueMw({ activeLimit: 1 }));

export default router;
index.routes.js
import { Router } from 'express';
import expressQueueMw from 'express-queue';

import chainRoutes from './endpoints/chain/chain.route';

const router = Router(); // eslint-disable-line new-cap

// mount chaincode routes at /chain
router.use('/api/chain', chainRoutes);

export default router;
index.js (express)
import routes from './server/routes/index.routes'
...config

app.use('/', routes);

...config

Also tried router.use('/api/chain', chainRoutes, expressQueueMw({ activeLimit: 1 })); and queue({ activeLimit: 1 }) but the requests don't queue

alykoshin commented 6 years ago

1) you have misprint import chainRoutes from './endpoints/chain/chain.route'; while the file named chain.routes.js

2) router.route('/init').get(chainCtrl.init, expressQueueMw({ activeLimit: 1 })); Here chainCtrl.init must be middleware in order to pass request further to next middleware i.e. to expressQueueMw, while I assume that according to its name, 'controller' will not use next() function. Maybe you need another order: router.route('/init').get(expressQueueMw({ activeLimit: 1 }), chainCtrl.init); ?

It looks working if I rewrite this way and call next(); inside chainCtrl.init:

https://github.com/alykoshin/express-queue/blob/e5c75395bf0ec32e1fa4513978a69f5d17cd8c61/examples/github-issues/1/server/routes/endpoints/chain/chain.routes.js#L9-L17

I added you example to /examples/gtihub-issues/1/ (rewritten to native Nodce's modules). You may update the module to latest version and find it inside package's directory node_modules/express-queue/ Then run it with 'DEBUG=* node index.js'

Javierdisertel commented 2 years ago

Thank you very much, you saved my day