Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app.use conditionally #1

Open
kivervinicius opened this issue Aug 9, 2016 · 7 comments
Open

app.use conditionally #1

kivervinicius opened this issue Aug 9, 2016 · 7 comments

Comments

@kivervinicius
Copy link

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

@alykoshin
Copy link
Owner

alykoshin commented Aug 9, 2016

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
Copy link
Author

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
Copy link
Owner

alykoshin commented Aug 9, 2016

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

@kivervinicius
Copy link
Author

Oh tks, i will test.

@DalderupMaurice
Copy link

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 alykoshin reopened this Apr 3, 2018
@alykoshin
Copy link
Owner

  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:

console.log('delayedResponse: enter');
setTimeout(() => {
console.log('delayedResponse: done');
res.send('delayedResponse: done');
}, 10000);
};
const router = Router(); // eslint-disable-line new-cap
router.route('/init').get(chainCtrl.init, expressQueueMw({ activeLimit: 1 }), delayedResponse);

I added you example to <package_dir>/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
Copy link

Thank you very much, you saved my day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants