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
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.
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).
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With