Express is the best template for transferring data between intermediate functions.

I just raised this issue with Express, and I'm interested in what StackOverflow thinks about it:

https://github.com/strongloop/express/issues/2831

My question is why Express forbids the developer from transferring data directly between intermediate functions and basically forces you to assign temporary data to the request object in what I always considered a rather inconvenient destination.

more specific:

Transferring data between intermediate functions usually involves doing this.

req.specialData = {}
next();

however, perhaps it would be much simpler and more productive (!) if it were possible

next(null,data);

or

function mySpecialMiddleWare(req,res,next,data){}

// now call the function above

mySpecialMiddleWare(req,res,next, {some:data});

, Express , , , , , function.length > 3 function.length === 4...

, , - ?

///, , req??

, , ?

+4
2

, Express , .

, API - , , . , - , , . , API . . , , , , .

, . , , , cookie. , , . , , .

, , - , arity ( ) API- TJ. , , API.

///, , req??

- , . , . , hapi, restify .., .

- , . .

- , . , , , , .

- "" , , , , , - JavaScript, TypeScript Haskell "" .

+5

, Express , , , Express middlewares res.locals. . Expressjs-plus. .

javascript, . (varInResLocals, varInReqParams, ) .

.

 var express = require('express');
 var ExpressPlus = require('expressjs-plus').ExpressPlus;
 var app = express();
 // simple handler example
 var userHandler = function(param, paramsArray, req, res){
    if(param !== 'user') return false;
    paramsArray.push("USER WAS FOUND!");
    return true;
};

 // this handler allows you to pass res.locals properties between your middlewares seemingly,
 // it the parameter was found in locals, it attaches it to paramsArray.
 var resLocalsHandler = function(param, paramsArray, req, res){
    if(param in res.locals){
        paramsArray.push(res.locals[param]);
        return true;
    }else return false;
};
 var appPlus = new ExpressPlus(app, [userHandler, resLocalsHandler], []);
 var regularFunction = function(user, id, cb){
    return cb(null, { response: {user: user, id: id}, status: 200, resLocalsVar: "passVar" });
};

 // resLocalsVar was passed in a previous method
 var regularFunction2 = function(resLocalsVar, user, id, cb){
 // now you can have access to it
    console.log(resLocalsVar);
    return cb(null);
};

 // the responder at the end will use res.locals.status and res.locals.response to issue an HTTP response
 app.use(appPlus.GMV(regularFunction), appPlus.GMV(regularFunction2), appPlus.responder);

 // adds error handlers, it will add a default error handler along with the list of error handlers passed
 // in this case, no error handlers were passed
 appPlus.setErrorHandlers();

 app.listen(3001, function(){
    console.log('Listening!');
});    
+3

Source: https://habr.com/ru/post/1620652/


All Articles