使用 Nest Framework 庆祝 npm 包
celebrate npm package with Nest Framework
有没有什么方法可以使用 celebrate NPM package with Nest Framework? In the documentation they only refer to class-validator, but although this has many advantages I have used with Express and other frameworks the celebrate middleware to validate the request. In the case of Nest, the middleware configuration is done in the app.module.ts file, but in others as routing-controllers 装饰器 @UseBefore 用于控制器中的中间件,这就是为什么我会感谢任何解释、示例或文档如何将此中间件与 Nest 一起使用。谢谢!!
解决方法:
在专用于验证的文件中创建 middleware
和 scheme
供 celebrate
使用(这将是一个简单的身份验证示例):
import { celebrate, Joi } from 'celebrate';
export const authenticate = celebrate({
headers: Joi.object().keys({
client: Joi.string().required(),
application: Joi.string().required(),
}).unknown(),
body: Joi.object().keys({
email: Joi.string().email().required(),
password: Joi.string().required(),
}),
});
以后怎么用?将其导入模块并将其作为 middleware
应用,因为 celebrate
returns 类型为 function (req, res, next)
:
的函数
import { Module, NestModule, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
// Others imports ...
import { authenticate } from './validations/authenticate.validation';
@Module({
imports: [ // Your imports... ],
controllers: [ // Your controllers... ],
providers: [ // Your providers... ],
})
export class AuthenticationModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
// Validate Login.
consumer.apply(authenticate).forRoutes({ path: 'security/login', method: RequestMethod.POST });
}
}
配置中定义的path
必须明显匹配controller的endpoint
:
import {
Body,
Post,
Headers,
Controller,
} from '@nestjs/common';
@Controller('security')
export class AuthenticationController {
@Post('login')
async authenticate(
@Body('email') email: string,
@Body('password') password: string,
@Headers('application') application: string): Promise<any> {
// Autenticate user
}
}
参考文献:
有没有什么方法可以使用 celebrate NPM package with Nest Framework? In the documentation they only refer to class-validator, but although this has many advantages I have used with Express and other frameworks the celebrate middleware to validate the request. In the case of Nest, the middleware configuration is done in the app.module.ts file, but in others as routing-controllers 装饰器 @UseBefore 用于控制器中的中间件,这就是为什么我会感谢任何解释、示例或文档如何将此中间件与 Nest 一起使用。谢谢!!
解决方法:
在专用于验证的文件中创建 middleware
和 scheme
供 celebrate
使用(这将是一个简单的身份验证示例):
import { celebrate, Joi } from 'celebrate';
export const authenticate = celebrate({
headers: Joi.object().keys({
client: Joi.string().required(),
application: Joi.string().required(),
}).unknown(),
body: Joi.object().keys({
email: Joi.string().email().required(),
password: Joi.string().required(),
}),
});
以后怎么用?将其导入模块并将其作为 middleware
应用,因为 celebrate
returns 类型为 function (req, res, next)
:
import { Module, NestModule, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
// Others imports ...
import { authenticate } from './validations/authenticate.validation';
@Module({
imports: [ // Your imports... ],
controllers: [ // Your controllers... ],
providers: [ // Your providers... ],
})
export class AuthenticationModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
// Validate Login.
consumer.apply(authenticate).forRoutes({ path: 'security/login', method: RequestMethod.POST });
}
}
配置中定义的path
必须明显匹配controller的endpoint
:
import {
Body,
Post,
Headers,
Controller,
} from '@nestjs/common';
@Controller('security')
export class AuthenticationController {
@Post('login')
async authenticate(
@Body('email') email: string,
@Body('password') password: string,
@Headers('application') application: string): Promise<any> {
// Autenticate user
}
}
参考文献: