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

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论