express 4 使用session和cookies
https://my.oschina.net/u/1466553/blog/294336
http://blog.csdn.net/liyi109030/article/details/35271389
express是基于node.js的一个web框架,但是到了4.xx版本之后,session管理和cookies等许多模块都不再直接包含在express中,而是需要单独下载添加。
var express = require('express');
var session = require('express-session'); //如果要使用session,需要单独包含这个模块
var cookieParser = require('cookie-parser'); //如果要使用cookie,需要显式包含这个模块
var RedisStore = require('connect-redis')(session);
var app = express();
// 设置 Cookie
app.use(cookieParser('xiaocc_'));
// 设置 Session
app.use(session({
store: new RedisStore({
host: "127.0.0.1",
port: 6379,
db: "test_session"
}),
resave:false,
saveUninitialized:false,
secret: 'keyboard cat'
}))
在express 4.0之前的版本,像session之类的中间件是伴随express自动安装的,网上有的教程使用的express版本正是4.0之前的版本,所以在使用4.0及其之后版本的时候一定要注意这点。
我的代码中使用session中间件的代码:
查看node_modules\connect-mongo\Readme.md可以看到4.0之后的用法已经不一样了。内容:
【解决办法】:
在package.json中加入express-session的中间件,命令提示符进入项目目录下,npm install 重新安装。或是在目录下使用命令安装:npm i express-session即可将express-session安装到项目中。在使用的模块中加入
……
使用的时候不在使用express.session,而是直接使用session():
app.use(session({
secret:setting.cookieSecret,
key:setting.db,
cookie:{maxAge:1000*60*60*24*30},
store:new MongoStore({db:setting.db})
}));
express 4 使用session和cookies的更多相关文章
- express 4.2.0 使用session和cookies
express 4.2.0 使用session和cookies https://my.oschina.net/u/1466553/blog/294336
- express 框架之session
一.什么是session? 最近在学习node.js 的express框架,接触到了关于session方面的内容.翻阅了一些的博客,学到了不少东西,发现一篇博文讲的很好,概念内容摘抄如下: Sessi ...
- ASP.Net MVC Session和Cookies的简单使用
目标:用Session和Cookies实现登陆信息保存和展现 Cookies实现: Controller: //把登陆用户名存到cookies中 HttpCookie cook = new HttpC ...
- URL、Session、Cookies、Server.Transfer、Application和跨页面传送,利弊比较
URL.Session.Cookies.Server.Transfer.Application和跨页面传送.-本题考查面试者对ASP.NET中多页面传值的理解是否全面.因为ASP.NET的页面表单提交 ...
- Session 与cookies 的区别
两个都可以用来存私密的东西,同样也都有有效期的说法. 区别在于:session是放在服务器上的,过期与否取决于服务期的设定,cookie是存在客户端的,过去与否可以在cookie生成的时候设置进去. ...
- express学习之session
最新版本的express没有包含session依赖项,需要我们自己配置并安装. 安装方法:npm install express-session 使用:var session = require('e ...
- 什么是,session、cookies、token
在一片文章上看到关于cookies和session 的解释,大概意思是这样的. 说你去星巴克喝咖啡,你平均一星期去两次,你的操作是看菜单,点咖啡.结账 加入你三星期不去,店员也许不及得你了,你依旧需要 ...
- express框架之session 存储
1.express-session 是基于express框专门用于处理session的中间件.这里不谈express-session怎么安装,只给出相应的实例代码.另外,session的认证机制离不开 ...
- Servlet中(Session、cookies、servletcontext)的基本用法
/req: 用于获得客户端(浏览器)的信息 //res: 用于向客户端(浏览器)返回信息 1.session的设置: //得到和req相关联的session,如果没有就创建ses ...
随机推荐
- python子线程退出
def thread_func(): while True: #do something #do something #do something t=threading.Thread(target = ...
- 最短路(sp
#include<stdio.h> #include<iostream> #include<queue> using namespace std; #define ...
- java OOP第03章_继承、抽象类和抽象方法
一. 为什么需要继承: 若多个类中都需要一些属性和方法,那么就可以将属性和方法抽取到一个父类中,需要的子类可以通过extends关键字去继承这个父类后拥有相应的属性和方法. 类.数组.方法----引用 ...
- kerberos协议介绍
一.kerberos认证过程: client向kerberos服务请求,希望获取访问server的权限.kerberos得到了这个消息,首先得判断client是否是可信赖的,也就是白名单黑名单的说法. ...
- 利用msbuild白名单执行shellcode
x64:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe x32:C:\Windows\Microsoft.NET\Framewo ...
- mysql出现You can’t specify target table for update in FROM clause
在mysql执行下面语句时报错: You can’t specify target table for update in FROM clause UPDATE edu_grade_hgm_1 ' W ...
- 【JZOJ3424】粉刷匠
description 赫克托是一个魁梧的粉刷匠,而且非常喜欢思考= = 现在,神庙里有N根排列成一直线的石柱,从1到N标号,长老要求用油漆将这些石柱重新粉刷一遍.赫克托有K桶颜色各不相同的油漆,第i ...
- 分享安装Apache、MySQL、PHP、LAMP的完整教程
Operation timed out after 30000 milliseconds with 0 out of -1 bytes received请注意,在Linux中输入密码时,不会显示您输入 ...
- Windows 隐藏控制台
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"& ...
- 学无止境,我爱python
每天更新.... python基础 python字符编码 python基础数据类型 python列表.元组 python字典 python字符编码unicode,utf-8,ascii python深 ...