Typescript express middleware Typescript express middleware express express

Typescript express middleware


The following should work fine i.e. use fat arrow for DoSomething not DoSomethingPrivate:

import * as express from 'express';var app = express();class Test {        constructor() {            }    // important:     DoSomething = (req:express.Request, res:express.Response, next:express.NextFunction) => {               this.DoSomeThingPrivate();    }    private DoSomeThingPrivate() :void    {           }}var test = new Test();app.use(test.DoSomething);

Note: You should not need to use bind. Also https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1


You've passed a reference just to the function itself. The function's instance will be global. You need to bind the function to the instance of test.

app.use(test.DoSomething.bind(test));