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?
Asked
Active
Viewed 1,106 times
2 Answers
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
-
Hi @Odunsi, do you have a working example of that ? Cannot find a way to make things work on my side... Thanks ! – Louis Charles Aug 29 '22 at 18:43