当前位置:网站首页>Nestjs配置服务,配置Cookie和Session
Nestjs配置服务,配置Cookie和Session
2022-07-03 10:31:00 【Johnny丶me】
关于NestJs中的服务
- 我们之前在Angular中用到的服务,在NestJs中也借鉴了相关思想
- 在NestJs中的服务可以是Service 也可以是Provider, 都可通过constructor注入依赖关系
- NestJs中的服务,相当于MVC中的Model, 它的本质是通过@Injectable()装饰器注解的类
- 在NestJs中的MVC中的V(View), 发起http请求后,被C(Controller)接收并返回信息给V
- 在C中的业务逻辑处理应该保持简洁和简单,在M中处理比较复杂的逻辑
内置服务
在我们生成模板项目的时候,已经有一个服务了:app.service.ts
@Injectable() export class AppService { getHello(): string { return 'Hello World!'; } }这个服务其实是一个类,是一个被@Injectable装饰器装饰的类,在app.module.ts中引入并配置
如果哪里用到这个服务,就在哪里注入,比如在app.controller.ts中去调用服务里面的方法
import { Controller, Get } from '@nestjs/common'; import { AppService } from './app.service'; @Controller() export class AppController { constructor(private readonly appService: AppService) { } @Get() getHello(): string { return this.appService.getHello(); } }
创建服务
- 创建服务有两种方式:
- $
nest g service cart - $
nest g provicer cart
- $
- 一般常用service来创建,在我们开发中创建模块,一般都会同时创建该模块的控制器和服务
- 我们也同样使用快捷命令创建,之后写我们的业务逻辑就流畅了
- 如果我们有通用的业务逻辑,一般会写一个通用的service
- 而且,如果有需要,我们可以在任何控制器中引入当前服务
- 一般我们在控制器中,先引入该服务,注入的时候基于constructor
constructor(private cartService:CartService){}- 之后调用的时候,直接通过 this.cartService.xxxx 来调用
开发模板结构化配置
- 基于我们创建的模板的结构都是比较松散的,后期创建的控制器和服务都是高内聚到单独模块里的
- 其实我们也可以将控制器都放到一起,服务都放到一起,但是个人感觉这样维护起来,并没有模块化的好
- 这些结构见仁见智,习惯怎样维护,只要方便查找即可
Nestjs中的cookie
1 ) cookie的初步使用
对用户信息进行标识,让用户多个页面信息共享,这时候我们就要基于cookie和session来处理
Nestjs默认使用express的HTTP配置时,我们可以安装express相关的插件,比如:cookie-parse
$
yarn add cookie-parser, 在main.ts中进行引入,并配置中间件// main.ts import * as cookieParser from 'cookie-parser'; // 配置cookie中间件 app.use(cookieParser()); // 注意cookieParser中最好传入一个密钥, 可以做加密,也可以不启用加密在控制器中测试cookie
import { Get, Response} from '@nestjs/common'; @Controller('user') export class ArticleController { @Get() index(@Response()) res { // 设置cookie 有效期1天,只有后端可读取cookie res.cookie("username", "Joh", { maxAge: 1000 * 60 * 60 * 24, httpOnly: true}) res.send(''); // return ''; // 注意,一定要用send,而不能return, 否则无法响应 } // 读取cookie @Get('cookie') getCookie(@Request() req) { console.log(req.cookies.username); return 'req.cookies.username'; } }cookie 常用配置如下
domain String 域名 expires Date 过期时间(second),时间点后cookie失效 httpOnly Boolean 通过程序(js脚本,applet)无法读取cookie, 防止XSS攻击 maxAge String 最大失效时间(毫秒) 设置在多少后失效 secure Boolean 如果设置true, 则只在https中生效 path String cookie影响到的路径,默认根路径 / 如果设置的路径不匹配,则无法携带cookie signed Boolean 是否签名cookie, 设置为true则对这个cookie签名,需要用到res.signedCookies 而非res.cookies访问;被篡改后的签名会被服务器拒绝,cookie并会被重置
2 ) cookie的加密处理
app.use(cookieParser(‘您的密钥’));
import { Get, Response} from '@nestjs/common'; @Controller('user') export class ArticleController { @Get() index(@Response()) res { // 设置cookie 有效期1天,httpOnly只有后端可读取cookie,signed加密cookie res.cookie("username", "Joh", { maxAge: 1000 * 60 * 60 * 24, httpOnly: true, signed: true}) res.send(''); // return ''; // 注意,一定要用send,而不能return, 否则无法响应 } // 读取cookie @Get('cookie') getCookie(@Request() req) { console.log(req.signedCookies.username); // 获取加密后的cookie, 如果不这样写,按正常写则会是undefined return 'req.cookies.username'; } }注意,使用request对象时,如果基于Render的装饰器来处理模板引擎,则可以使用return来包装数据
import { Get, Response, Render} from '@nestjs/common'; @Controller('user') export class ArticleController { @Get() @Render('default/user'); index(@Response()) res { // 设置cookie 有效期1天,httpOnly只有后端可读取cookie,signed加密cookie res.cookie("username", "Joh", { maxAge: 1000 * 60 * 60 * 24, httpOnly: true, signed: true}); // 注意这里直接 return return { name: 'Joh'}; } }
Nestjs中的session
- session用于记录客户状态,和cookie配合使用
- 当客户端浏览器第一次访问服务器时,会在服务器端创建一个session对象,生成一个类似key,value的键值对,然后将key(cookie)返回到浏览器端
- 浏览器再次访问时,将会携带此key(cookie),在服务器端找对对应的value(session)
- 这个过程类似于客户用信用卡或身份证和银行柜台柜员进行沟通一样
1 )安装express-session
- $
yarn add express-session
2 ) 配置 express-session
import * as session from 'express-session';
app.use(session({
secret: 'keyboard cat', cookie: {
maxAge: 1000 * 60 * 60 * 24}}));
3 ) 使用session
// 这里只写关键代码
index(@Request() req): string {
// 设置session
req.session.username = req.signedCookies.username;
}
4 ) session的常用配置
app.use(
session(
{
name: 'c.sid', // 返回客户端的 key 的名称,默认为 connect.sid,也可以自己设置
secret: 'keyboard cat', // 密钥,一个 String 类型的字符串,作为服务器端生成 session 的签名
resave: false, // 强制保存 session 即使它并没有变化,。默认为 true。建议设置成 false
saveUninitialized: true, // 强制将未初始化的 session 存储。当新建了一个 session 且未设定属性或值时,它就处于未初始化状态。在设定一个cookie前,这对于登陆验证,减轻服务端存储压力,权限控制是有帮助的。(默 认:true)。
cookie: {
maxAge: 1000 * 60 * 60 * 24} // cookie配置,设置返回到前端 key 的属性,默认值为{ path: ‘/’, httpOnly: true, secure: false, maxAge: null }。
rolling: true, // 在每次请求时强行设置 cookie,重置 cookie 过期时间(默认:false)
}
)
);
express-session 的常用配置
// //设置 session
req.session.username='Joh';
// 获取 session
req.session.username;
// 重新设置 cookie 的过期时间
req.session.cookie.maxAge=0;
// 销毁 session
req.session.destroy(function(err) {
/* ToDo */ })
边栏推荐
- Function details of CorelDRAW graphics suite 2022
- Linear table sequence table comprehensive application problem p18
- Double linked list of linear list
- Hal -- writing hardware drivers
- 一文搞懂Go语言Context
- ORACLE进阶(一) 通过EXPDP IMPDP命令实现导dmp
- Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable
- Kotlin's use of the no Arg compiler plug-in in gradle
- Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power
- P3250 [hnoi2016] Network + [necpc2022] f.tree path tree section + segment tree maintenance heap
猜你喜欢

FL Studio 20 unlimited trial fruit arranger Download

面试题总结(2) IO模型,集合,NIO 原理,缓存穿透,击穿雪崩

After using the thread pool for so long, do you really know how to reasonably configure the number of threads?

How to clean up v$rman_ backup_ job_ Details view reports error ora-02030

(二)进制

AMS Series 1 - AMS startup process

数据库增量备份 - DB INCR DB FULL

面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩

高精度室内定位技术,在智慧工厂安全管理的应用

Leetcode 46: full arrangement
随机推荐
Matlab extracts numerical data from irregular txt files (simple and practical)
Résumé des questions d'entrevue (2) Modèle io, ensemble, principe NiO, pénétration du cache, avalanche de rupture
Technical experts from large factories: how can engineers improve their communication skills?
解决undefined reference to `__aeabi_uidivmod‘和undefined reference to `__aeabi_uidiv‘错误
Programmers' entrepreneurial trap: taking private jobs
Processes and threads
2022 northeast four provinces match VP record / supplementary questions
如何成为一名高级数字 IC 设计工程师(1-5)Verilog 编码语法篇:操作数
Key switch: press FN when pressing F1-F12
2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func mai
大厂技术专家:工程师如何提升沟通能力?
Spl06-007 air pressure sensor (example of barometer)
在腾讯云容器服务Node上执行 kubectl
今晚要修稿子準備發佈。但是,仍卡在這裡,也許你需要的是一個段子。
MATLAB提取不規則txt文件中的數值數據(簡單且實用)
AIDL
一文搞懂Go语言Context
P3250 [hnoi2016] Network + [necpc2022] f.tree path tree section + segment tree maintenance heap
软考中级软件设计师该怎么备考
基于I2C协议的驱动开发