当前位置:网站首页>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 */ })
边栏推荐
- Hal -- writing hardware drivers
- Redis things
- Google Earth engine (GEE) - ghsl global population grid dataset 250 meter resolution
- 按键切换:按F1-F12都需要按Fn
- VPP three-layer network interconnection configuration
- Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
- Arctangent entropy: the latest SCI paper in July 2022
- The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities
- 一些常用术语
- MATLAB提取不规则txt文件中的数值数据(简单且实用)
猜你喜欢

基于I2C协议的驱动开发

Driver development based on I2C protocol

一文搞懂Go语言Context

软件测试周刊(第78期):你对未来越有信心,你对现在越有耐心。

Abandon the Internet after 00: don't want to enter a big factory after graduation, but go to the most fashionable Web3

Redis things

Analysis of EPS electric steering system

A simple method of adding dividing lines in recyclerview

ASP.NET-酒店管理系統

Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests
随机推荐
Oracle收回权限 & 创建角色
Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable
一些常用术语
图解网络:什么是虚拟路由器冗余协议 VRRP?
按键切换:按F1-F12都需要按Fn
Multi dimensional monitoring: the data base of intelligent monitoring
Hal - General
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
Empire CMS no thumbnail smart tag (e:loop) two ways to judge whether there is a titlepic
C language two-dimensional array
Spl06-007 air pressure sensor (example of barometer)
浅析-JMM内存模型
FL Studio 20无限试用版水果编曲下载
软考中级软件设计师该怎么备考
A simple method of adding dividing lines in recyclerview
帝国cms 无缩略图 灵动标签(e:loop)判断有无标题图片(titlepic)的两种写法
one hot 独热码
How to make others fear you
AOSP ~ NTP ( 网络时间协议 )
[OBS] encapsulate the basic process of OBS acquisition