简体   繁体   English

如何在tsoa express中使用csurf中间件?

[英]How to use csurf middleware in tsoa express?

I am new to tsoa and I want to do CSRF implementation in my node app.我是 tsoa 的新手,我想在我的节点应用程序中执行 CSRF。 I have been able to make api using app.use() but I want to write in tsoa .我已经能够使用app.use()制作 api 但我想写在tsoa中。 Is there any way?有什么办法吗?

In the pre-released version , you can use the @Middlewares() decorator.预发布版本中,您可以使用 @Middlewares() 装饰器。

Just put what you had in a app.use() to the @Middlewares() decorator.只需将app.use()中的@Middlewares()装饰器即可。

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 .还记得在tsconfig.json experimentalDecorators设置为true 1 1个


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM