How to use 'this' context in middleware

Erik

I wrote my own middleware as module for my purposes that looks like the following:

-- myMiddleware.js

module.exports = {
    fn1: function (req, res, next) {
       console.log('fn1');
       next();
    },

    fn2: function (req, res, next) {
        console.log('fn2');
        this.fn1(req, res, function () {
             next();
        });
    }
};

In my sserver.js I use this middleware as the following:

app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));

app.use(require('./myMiddleware').fn2);

Unfortunately that does not work because this context in fn2 in not myMiddleware.js object. How can use proper this?

Alfonso de la Osa

You can't, the 'this' property will always be the vanilla Server (http.Server) instance or compatible handler.

You can do this:

var middlewares = {};

module.exports = middlewares;

middlewares.fn1 = function(req, res, next){ ... };

middlewares.fn2 = function(req, res, next){ 
    middlewares.fn1(req, res, next); 
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to use the context in the tests?

From Dev

How to use context in a function?

From Dev

how to use the context in the tests?

From Dev

How should I unit test middleware packages with gorilla context

From Dev

How to use 'OR' middleware for route laravel 5

From Dev

Laravel 5.2 how to use config::set in middleware

From Dev

How to use middleware in Laravel for secure controllers?

From Dev

How to use Apache webserver as middleware component for OWIN?

From Dev

How to use middleware to instead of controller initialize?

From Dev

How to use middleware for multiple type of admins in laravel?

From Dev

how to use this context in angularjs views?

From Dev

How to use Context with flatMap() in Reactor?

From Dev

How to properly use jQuery "context"

From Dev

how to use this context in angularjs views?

From Dev

How to use the LocalizationUtility in test context

From Dev

How to use a variable in a different context?

From Dev

How to require middleware with a middleware?

From Dev

Is there a better way to use middleware in middleware?

From Dev

Express middleware run in another context?

From Dev

How can I use a variable that is defined in a turtle context in a patch context?

From Dev

React - How to use cloneElement so it preserves context

From Dev

How to use a Sqlalchemy Model out of a session context?

From Dev

How to fix "Illegal datatype context" (use -XDatatypeContexts)?

From Dev

How to use a std::mutex in a class context

From Dev

How to use Sequence labeling for queries with different context?

From Dev

How to use jQuery methods inside an iframe context

From Dev

NetLogo: how to use tick in correct context

From Dev

Context::getContext() how to use in Prestashop Bankwire module

From Dev

How to correctly use kivy graphics context instruction

Related Related

  1. 1

    how to use the context in the tests?

  2. 2

    How to use context in a function?

  3. 3

    how to use the context in the tests?

  4. 4

    How should I unit test middleware packages with gorilla context

  5. 5

    How to use 'OR' middleware for route laravel 5

  6. 6

    Laravel 5.2 how to use config::set in middleware

  7. 7

    How to use middleware in Laravel for secure controllers?

  8. 8

    How to use Apache webserver as middleware component for OWIN?

  9. 9

    How to use middleware to instead of controller initialize?

  10. 10

    How to use middleware for multiple type of admins in laravel?

  11. 11

    how to use this context in angularjs views?

  12. 12

    How to use Context with flatMap() in Reactor?

  13. 13

    How to properly use jQuery "context"

  14. 14

    how to use this context in angularjs views?

  15. 15

    How to use the LocalizationUtility in test context

  16. 16

    How to use a variable in a different context?

  17. 17

    How to require middleware with a middleware?

  18. 18

    Is there a better way to use middleware in middleware?

  19. 19

    Express middleware run in another context?

  20. 20

    How can I use a variable that is defined in a turtle context in a patch context?

  21. 21

    React - How to use cloneElement so it preserves context

  22. 22

    How to use a Sqlalchemy Model out of a session context?

  23. 23

    How to fix "Illegal datatype context" (use -XDatatypeContexts)?

  24. 24

    How to use a std::mutex in a class context

  25. 25

    How to use Sequence labeling for queries with different context?

  26. 26

    How to use jQuery methods inside an iframe context

  27. 27

    NetLogo: how to use tick in correct context

  28. 28

    Context::getContext() how to use in Prestashop Bankwire module

  29. 29

    How to correctly use kivy graphics context instruction

HotTag

Archive