question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to call check inside the middleware instead of passing as middleware

See original GitHub issue

I already have middleware already, I don’t like to use check as middlewares to this, is there anyway to use check inside reqHandler ? Like how we used with Legacy API ?

const reqHandler = (req, res, next) => {
// do something
};

router.post('/createTask', reqHandler);

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
vinczebalazscommented, Jul 27, 2018

For a more intuitive solution regarding multiple checks, you could also attach your checks to a router and call that router inside your middleware/request handler.

Something like so:

const express   = require('express');
const validator = express.Router();

// Add the checks (could also be done in a separate file)
validator.use(check('email').isEmail());
validator.use(check('password').not().isEmpty());

const reqHandler = (req, res, next) => {
    validator(req, res, next);
}
1reaction
chrisdlcommented, Jul 20, 2018

I would think for one check something like this should work.

const reqHandler = (req, res, next) => {
    check('password').not().isEmpty()(req, res, next);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to call a middleware function from inside ... - Stack Overflow
For this, I create a new middleware isLoggedIn . The problem here is that if I find the user is logged in, I...
Read more >
Writing middleware for use in Express apps
The middleware function myLogger simply prints a message, then passes on the request to the next middleware function in the stack by calling...
Read more >
How Node JS middleware Works? - Selvaganesh - Medium
Call the next middleware in the stack. If the current middleware function does not end the request-response cycle, it must call next() to...
Read more >
How to pass variables to the next middleware using next() in ...
The following example covers how to pass variables to the next middleware using next() in Express.js. Approach: We cannot directly pass data ...
Read more >
Build and Understand Express Middleware through Examples
In order to call an error-handling middleware, you simply pass the error to next() , like this: app.get('/my-other-thing' ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found