javascript - Nodejs and Connect "next" functionality - Stack Overflow

It seems that if I want to move to a "next" function in Nodejs (and possibly Javascript in ge

It seems that if I want to move to a "next" function in Nodejs (and possibly Javascript in general?) I cannot pass parameters to the next function.

Here is what I mean:

app.get('/webpage', SomeFunction, NextFunction);

function SomeFunction (req, res, next) {
    // Do things
    next();
}

function NextFunction (req, res) {
    // Do other things
}

Now, if in SomeFunction I were to say next(req, res); it does not seem to work. It never gets to the method. Obviously I cannot directly pass parameters...but my question is why? How does the next function know which parameters to use? Is it because they are named the same or does it automatically pass the 1st and 2nd parameters? If NextFunction used blah, bleet instead of req, res would it still work?

It seems that if I want to move to a "next" function in Nodejs (and possibly Javascript in general?) I cannot pass parameters to the next function.

Here is what I mean:

app.get('/webpage', SomeFunction, NextFunction);

function SomeFunction (req, res, next) {
    // Do things
    next();
}

function NextFunction (req, res) {
    // Do other things
}

Now, if in SomeFunction I were to say next(req, res); it does not seem to work. It never gets to the method. Obviously I cannot directly pass parameters...but my question is why? How does the next function know which parameters to use? Is it because they are named the same or does it automatically pass the 1st and 2nd parameters? If NextFunction used blah, bleet instead of req, res would it still work?

Share Improve this question edited May 25, 2023 at 15:50 aynber 23k9 gold badges54 silver badges68 bronze badges asked Aug 1, 2011 at 2:59 James P. WrightJames P. Wright 9,13123 gold badges82 silver badges144 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15

This is an intentional aspect of the design of Connect (the node.js middleware that's responsible for this behaviour). The next function your middleware receives is not the next middleware in the stack; it's a function that Connect generates which asks the next middleware to handle it (as well as doing some extra stuff to handle special cases, like when there isn't a "next middleware").

If your middleware should return a response, just do so. If it shouldn't, it's implied that some later middleware should return a response. If you need to pass along data to that later part of the process, you should attach it to an appropriate part of the request object req.

For example, the bundled bodyParser middleware is responsible for populating req.rawBody and req.body based on the contents of the request body. The bundled basicAuth middleware populates req.remoteUser based on the HTTP authentication.

This is the pattern you should try to emulate: a stack of middleware, each of which does a basic incremental thing to process the request. If what you're trying to model doesn't fit into this paradigm, then you should probably just have a single function to handle the request, from which you can call all of your own application logic however you like.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743781404a4505868.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信