Passing variables to the next middleware using next() in Express.js

后端 未结 6 1392
长情又很酷
长情又很酷 2020-11-27 10:11

Well, my question is I want to pass some variable from the first middleware to another middleware, and I tried doing this, but there was \"req.somevariable is a

相关标签:
6条回答
  • 2020-11-27 10:30

    That's because req and res are two different objects.

    You need to look for the property on the same object you added it to.

    0 讨论(0)
  • 2020-11-27 10:31

    Attach your variable to the req object, not res.

    Instead of

    res.somevariable = variable1;
    

    Have:

    req.somevariable = variable1;
    

    As others have pointed out, res.locals is the recommended way of passing data through middleware.

    0 讨论(0)
  • 2020-11-27 10:37

    As mentioned above, res.locals is a good (recommended) way to do this. See here for a quick tutorial on how to do this in Express.

    0 讨论(0)
  • 2020-11-27 10:45

    I don't think that best practice will be passing a variable like req.YOUR_VAR. You might want to consider req.YOUR_APP_NAME.YOUR_VAR or req.mw_params.YOUR_VAR.

    It will help you avoid overwriting other attributes.

    Update May 31, 2020

    res.locals is what you're looking for, the object is scoped to the request.

    An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any). Otherwise, this property is identical to app.locals.

    This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on.

    0 讨论(0)
  • 2020-11-27 10:52

    This is what the res.locals object is for. Setting variables directly on the request object is not supported or documented. res.locals is guaranteed to hold state over the life of a request.

    res.locals

    An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any). Otherwise, this property is identical to app.locals.

    This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on.

    app.use(function(req, res, next) {
        res.locals.user = req.user;  
        res.locals.authenticated = !req.user.anonymous;
        next();
    });
    

    To retrieve the variable in the next middleware:

    app.use(function(req, res, next) {
        if (res.locals.authenticated) {
            console.log(res.locals.user.id);
        }
        next();
    });
    
    0 讨论(0)
  • 2020-11-27 10:52

    The trick is pretty simple... The request cycle is still pretty much alive. You can just add a new variable that will create a temporary, calling

    app.get('some/url/endpoint', middleware1, middleware2);
    

    Since you can handle your request in the first middleware

    (req, res, next) => {
        var yourvalue = anyvalue
    }
    

    In middleware 1 you handle your logic and store your value like below:

    req.anyvariable = yourvalue
    

    In middleware 2 you can catch this value from middleware 1 doing the following:

    (req, res, next) => {
        var storedvalue = req.yourvalue
    }
    
    0 讨论(0)
提交回复
热议问题