最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is second parameter in nodejs post method - Stack Overflow

programmeradmin2浏览0评论

In my code I have code written like this

 router.post('/', publicShare, function(req, res, next) {

I check in documents but not found why second param publicShare is here?

publicShare is looks like

var publicShare = function(req, res, next) {
    if (condition1) {
        if (condition2) {
            res.status(400).send({success:false});
        } else {
            next();       
        }
    } else {
        if (condition3) {
            res.status(401).send({success:false});
        } else
            next();
    }
};

Please help me to understand.

In my code I have code written like this

 router.post('/', publicShare, function(req, res, next) {

I check in documents but not found why second param publicShare is here?

publicShare is looks like

var publicShare = function(req, res, next) {
    if (condition1) {
        if (condition2) {
            res.status(400).send({success:false});
        } else {
            next();       
        }
    } else {
        if (condition3) {
            res.status(401).send({success:false});
        } else
            next();
    }
};

Please help me to understand.

Share Improve this question edited Feb 9, 2019 at 16:19 GRESPL Nagpur asked Feb 9, 2019 at 16:13 GRESPL NagpurGRESPL Nagpur 2,0983 gold badges23 silver badges42 bronze badges 5
  • 1 It's middleware, per the API documentation. – jonrsharpe Commented Feb 9, 2019 at 16:16
  • 1 Well precisely its express middle ware function – Shubham Dixit Commented Feb 9, 2019 at 16:19
  • Thanks a lot @jonrsharpe and Shubh I am new in node js – GRESPL Nagpur Commented Feb 9, 2019 at 16:20
  • Now I read the text I was searching is A series of middleware functions (separated by mas). – GRESPL Nagpur Commented Feb 9, 2019 at 16:21
  • @ShubhDixit : Please help me to ignore that second middleware what I do ? just writing next(); at very first line of publicShare is not working ? – GRESPL Nagpur Commented Feb 9, 2019 at 16:25
Add a ment  | 

3 Answers 3

Reset to default 5

You can check route handlers which accepts array of callbacks which just behaves like a middleware. Example from the docs:

app.get('/example/d', [cb0, cb1], function (req, res, next) {

So, in your case publicShare can be array of callbacks or just a callback which signature is just a callback accepting req, res, and next as parameter. So, you can also use like:

app.get('/', function(req, res, next){}, function(req, res, next){}, ...

And for easier, you would use an array of callbacks:

app.get('/',[cb1, cb2, cb3])

Where cb1, cb2, and cb3 are the callbacks with request, response and next parameters. It allows you to run one by one. cb1 -> do log 1, then cb2 -> do log 2, cb3 -> do log 3 and so on.

I would simplify this with an example:

You would request for water.

1) cb1: Purchase a jar of water.

2) cb2: Add few water drops in the bucket or jar.

3) cb3: Boil it.

Then, it's your turn. Drink!

publicShare method in your route is a express middleware function .According to the docs

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle

A middleware checks for certain condition or criteria to be applied on your request and response object ,if the criteria fulfills then the next operation is done ,which is either

1-End the request response cycle

2-Call the next middleware function in the stack.

You can refer the docs for more information -https://expressjs./en/guide/using-middleware.html

According to the documentation, you can add multiple middleware functions separated by mas. The 'publicShare' variable must be a middleware function.

发布评论

评论列表(0)

  1. 暂无评论