nodejs之express中间件cookie-parser使用
知识点:
* 、domain的使用,.aaa.com的域名都共享这个cookie信息
* res.cookie('username',"cookie value",{maxAge:,domain:'.aaa.com'});
* 、获取所有cookie,设置cookie
* req.cookies / res.cookie("cookieName","cookieValue")
* 、path设置,使得cookie只能在/get路由下才可以获取
* res.cookie('username',"cookie value",{maxAge:,path:'/get',domain:'.aaa.com'});
* 、httpOnly:true
* res.cookie('username',"cookie value",{maxAge:,httpOnly:true,domain:'.aaa.com'});
* 表示只能在nodejs服务端可以操作cookie,不能使用js脚本操作
* 、signed:true
* cookie加密
* cookie加密:
* 、在保存的时候进行加密
* 、cookie-parser中间件中有一个signed:true属性
* 第一步:app.use(cookieParser(""));//使用signed时,要穿一个加密盐
* 第二部:res.cookie("username","cookie value",{maxAge:,signed:true});
* 第三部:console.log(req.signedCookies); 或者 console.log(req.signedCookies.username);
1、先安装express以及中间件cookie-parser
npm install express
npm install cookie-parser
2、案例如下
var express = require('express');
var cookieParser = require('cookie-parser'); //引用中间件
var app = new express(); //创建实例
app.use(cookieParser());//设置中间件
app.get('/set',function (req,res) {
res.cookie("username",'yangwenjie',{maxAge:}); //设置cookie和过期时间
res.send("set cookie sucessful");
})
app.get('/get',function (req,res) {
console.log(req.cookies); //拿取cookies
res.send("set cookie sucessful");
})
app.get('/',function (req,res) {
res.send("index");
})
app.listen('');
3、获取和设置cookie的demo
const cookieParser = require('cookie-parser');
const express = require('express');
const app = express();
app.use(cookieParser(""));//使用signed时,要穿一个加密盐
app.get('/',function (req,res) {
res.cookie("username","cookie value",{maxAge:,signed:true});//设置cookie
res.send('index');
})
app.get('/get',function (req,res) {
console.log(req.signedCookies.username);//获取特定cookie值
res.send(" get cookie value");
})
app.listen('');
nodejs之express中间件cookie-parser使用的更多相关文章
- nodejs之express中间件路由使用
1.express 中间件使用 /* * 中间件:就是匹配路由之前和匹配路由之后做的一系列操作 */ var express = require('express'); var app = new e ...
- nodeJs,Express中间件是什么与常见中间件
中间件的功能和分类 中间件的本质就是一个函数,在收到请求和返回相应的过程中做一些我们想做的事情.Express文档中对它的作用是这么描述的: 执行任何代码.修改请求和响应对象.终结请求-响应循环.调用 ...
- nodejs之express中间件body-parser使用
1.安装express和body-parser npm install express npm install body-parser 2‘.案例如下 var express = require('e ...
- 77.深入理解nodejs中Express的中间件
转自:https://blog.csdn.net/huang100qi/article/details/80220012 Express是一个基于Node.js平台的web应用开发框架,在Node.j ...
- nodejs之express的中间件
express中间件分成三种 内置中间件 static 自定义中间件 第三方中间件 (body-parser) (拦截器) 全局自定义中间件 在请求接口时 有几个接口都要验证传来的内容是否存在或者是否 ...
- express中间件的理解
参考 :https://blog.csdn.net/huang100qi/article/details/80220012 Express中间件分为三种内置中间件.自定义中间件.第三方中间件 可以与n ...
- NodeJS 框架 Express 从 3.0升级至4.0的新特性
NodeJS 框架 Express 从 3.0升级至4.0的新特性 [原文地址:√https://scotch.io/bar-talk/expressjs-4-0-new-features-and-u ...
- NodeJS with Express 4.x
Express 4.x 静态资源目录设置: //静态文件目录 app.use('/public', express.static(__dirname+'/public')); app.use('/da ...
- [转] NodeJS框架express的途径映射(路由)功能及控制
NodeJS框架express的路径映射(路由)功能及控制 我们知道Express是一个基于NodeJS的非常优秀的服务端开发框架,本篇CSSer将提供express框架的route和route co ...
随机推荐
- deep_learning_Function_matpotlib_scatter()函数
plt.scatter()函数用于生成一个scatter散点图. matplotlib.pyplot.scatter(x, y, s=20, c='b', marker='o', cmap=None, ...
- linux pip使用国内源
最近在Linux里面使用pip安装应用的速度十分的慢,于是便上网找了一些国内的源. 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:https:// ...
- 【LeetCode】451-根据字符出现频率排序
题目描述 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r'和' ...
- const与constexpr
关于const型数据,谭浩强老爷子这么总结道: Time const t; //t是常对象,其值在任何情况下都不能改变 void Time::fun()const; //fun是Time类中的常成员函 ...
- Daily Affirmations 每天对自己大声说:我很棒
I was 18 the first time a therapist2) tried to get me to embrace the idea of daily affirmations. I w ...
- Python&R:警告信息管理
计算机程序有时很人性化,比如给你警告提示信息: 计算机程序有时又非常不人性化,比如动不动就给你警告提示...... 如果你的程序是要给客户使用,有运行美化要求: 再尤其是比如警告出现在循环里的情况,那 ...
- Kubernetes 基本概念和术语
Kubernetes 基本概念和术语 Kubernetes 中大部分概念如 Node.Pod.Replication Controller. Service 等都可以看做一种 "资源对象&q ...
- 23.git简单使用
git:我主要是为了收集命令,学习请去看廖雪峰的博客,内容很详细 git客户端下载: Git是GitHub开源社区的版本管理系统: 下载地址:https://git-scm.com/download/ ...
- JavaScript分支结构Ⅰ—IF-ELSE
㈠程序 ⑴程序的流程控制 程序=数据+算法 ⑵程序的三种结构: ①顺序结构 ②分支结构 ③循环结构 ㈡IF结构 ⑴什么是分支结构? 程序在运行过程中,根据不同的条件,选择执行某些语句 ⑵什么是IF结构 ...
- re.compile 函数
re.compile 函数 compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 search() 这两个函数使用. 语法格式为: re.com ...