Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript express middleware

I have a simple auth middleware for express. It checks header and if all cool it calls next()

Now when i am in "DoSomething" "this" is equal to global and not the instance of "Test" and "this.DoSomeThingPrivate" is undefined.

I have tried the

DoSomeThingPrivate :() => void;

this.DoSomeThingPrivate = () => {
...
}

pattern. But also does not work.

import express = require('express');

var app = express();

class Test {    
    constructor() {        
    }

    DoSomething(req:express.Request, res:express.Response, next:Function) :void {       
        this.DoSomeThingPrivate();
    }

    private DoSomeThingPrivate() :void
    {       
    }
}

var test = new Test();

app.use(test.DoSomething);

Relates to this

Any Ideas...

thanks

like image 847
Pintac Avatar asked Dec 19 '14 13:12

Pintac


People also ask

What is typescript and how to use it?

It is a superset of JavaScript with additional capabilities, most notable being static type definitions making it an excellent tool for a better and safer development experience. Let us first add support for TypeScript to our Node.js project and then see a snippet of the middleware functions written using the TypeScript language.

How to use middleware in express JS?

In the case of express.js, this kind of middleware are bound to the application by using app.use(). This means a middleware will be executed for all endpoints set in the application, unless we specify a specific path. We can use application-level middleware in express by using app.METHOD() or a request method (GET, POST, PUT, DELETE).

Where can I find Express-typescript?

It is available at this address: mwanago/express-typescript. You can use it as a boilerplate if you would find it useful. Express is a framework for Node.js used to build the backend for web applications. It is unopinionated, meaning that you can use it in a manner in which you see fit.

Can you use typescript Express as a boilerplate?

You can use it as a boilerplate if you would find it useful. Express is a framework for Node.js used to build the backend for web applications. It is unopinionated, meaning that you can use it in a manner in which you see fit. In this tutorial, I present a way that works for me while working with the TypeScript Express.


1 Answers

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

like image 72
basarat Avatar answered Nov 14 '22 22:11

basarat