1

I am new to tsoa and I want to do CSRF implementation in my node app. I have been able to make api using app.use() but I want to write in tsoa. Is there any way?

Sammy
  • 15
  • 6

2 Answers2

1

Just put what you had in a app.use() to the @Middlewares() decorator.

You can define your Middleware / Middlewares like this:

import { Request, Response, NextFunction } from 'express';

// ... controller logic ...

// @Get('/endpoint') | @Post('/endpoint') etc.
@Middlewares([
  (req: Request, res: Response, next: NextFunction) => {
    console.log(req.headers);
    next();
  },
  (req: Request, res: Response, next: NextFunction) => {
    console.log('Second middleware, but we can also use only one!');
    next();
  },
])
// getEndpoint(): string {
//   return 'Hello World!';
// }

// ... controller logic ...

Also remember to have set the experimentalDecorators to true in your tsconfig.json.1


1 https://github.com/lukeautry/tsoa/pull/1123#issuecomment-1018251162

rakso
  • 35
  • 1
  • 5
0

In the pre-released version, you can use the @Middlewares() decorator.

Odunsi
  • 421
  • 1
  • 4
  • 16