500 TypeError: Cannot read property 'connect.sid' of undefined
1:在写passport验证测试用例时,发现有几个引用中间件顺序的错误,检查发现,passport验证写的是session,在传错误信息的时候req.flash调用也需要用到session中间件,否则会报错:

我先引用 app.use express.session({secret: 'keyboard'})
2:引用后,运行代码,又报错:

由于会话是使用cookie实现的,因此还得引用cookie中间件
app.use express.cookieParser()
cookie一定要写到session中间件的前面,原因:
Middleware order matter in Express, as they are executed in the order you defined them. In your case, the cookieParser middleware adds some information that is used by the session middleware.
You can check connect's documentation:
Session data is not saved in the cookie itself, however cookies are used, so we must use the cookieParser() middleware before session().
3: 发现了个很妖的情况,在form窗里里submit后,input输入框的数据居然没有带到body里,每次body都是空的,检查出来发现urlencoded中间件居然被我屏蔽掉了,好火大,
app.use express.json()
app.use express.urlencoded()
其实 bodyParser() 支持 JSON, urlencoded和multipart requests的请求体解析中间件。 这个中间件是json(), urlencoded(),和multipart() 这几个中间件的简单封装
app.use(express.bodyParser()); // 等同于:
app.use(express.json());
app.use(express.urlencoded());
app.use(express.multipart());
那为什么不用express.bodyParser来的方便????????当我使用bodyParser()替换时,运行警告:
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
原来connect移除了所有中间件,connect.multipart()更是访问不到了,express版本: "version": "3.5.1", 难怪用express自动建项目的时候直接使用的app.use express.json();app.use express.urlencoded()
其他人的解释:connect 发布了 3.0, 移除了所有中间件。express 发布了 4.0,移除了 connect 的依赖。express 老版本引用了 connect 2.x,所以会有这个提示。
express.multipart()中间件用于文件上传,如果没有文件上传建议就别用了
在做一个文件上传的时候,需要用到req.files这个属性,如果不用app.use(express.bodyParser())这个代码就会报错了,这个时候是不是只能回到express的旧版本呢????
500 TypeError: Cannot read property 'connect.sid' of undefined的更多相关文章
- 微信小程序开发——使用回调函数出现异常:TypeError: Cannot read property 'setData' of undefined
关键技术点: 作用域问题——回调函数中的作用域已经脱离了调用函数了,因此需要在回调函数外边把this赋给一个新的变量才可以了. 业务需求: 微信小程序开发,业务逻辑需要,需要把获取手机号码的业务逻辑作 ...
- vue.common.js?e881:433 TypeError: Cannot read property 'nodeName' of undefined
我觉得吧,是这么个原因,就是响应式要找这个node改它的内容,没找着,就报错了. 用computed监控vuex的state属性,绑定到页面上,如果这个属性改了,因为响应式,那么就要更改页面,如果页面 ...
- Uncaught TypeError: Cannot read property 'msie' of undefined
因为图方便,抄了别人写的一个jquerry插件,运行时“var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ...
- Uncaught TypeError: Cannot read property 'insertAdjacentHTML' of null
在开发Ext 项目中如果遇到 Uncaught TypeError: Cannot read property 'insertAdjacentHTML' of null 这个错误,检查下renderT ...
- easyui使用时出现这个Uncaught TypeError: Cannot read property 'combo' of undefined
easyui使用时出现这个Uncaught TypeError: Cannot read property 'nodeName' of undefined 最后检查发现是必须给select一个id,光 ...
- Uncaught TypeError: Cannot set property 'innerHTML' of null
学习Chrome插件时,要在弹出页面中显示当前时间,结果怎样也显示不出来 看了 http://www.cnblogs.com/mfryf/p/3701801.html 这篇文章后感悟颇深 通过调试发现 ...
- Uncaught TypeError: Cannot read property 'call' of undefined jquery.validate.min.js:28
最近在做表单验证时,,自己写的addMethod 方法总是不起作用.折腾了将近一天. 报告的错误,如下面的 Uncaught TypeError: Cannot read property 'call ...
- reactjs Uncaught TypeError: Cannot read property 'location' of undefined
reactjs Uncaught TypeError: Cannot read property 'location' of undefined reactjs 路由配置 怎么跳转 不成功 国内搜索引 ...
- index.js:13 Uncaught TypeError: Cannot read property 'split' of undefined
使用 webpack 编译 Vue 项目时出现报错: index.js:13 Uncaught TypeError: Cannot read property 'split' of undefined ...
随机推荐
- QT 加载c语言编译的动态库
QLibrary lib("./libprint.so");//库的路径if(lib.load()){ typedef void(*AddFunction)(char *st ...
- 用iconv指令解决utf8和gb18030编码间转换
Linux显示在Windows编辑过的中文就会显示乱码是由于两个操作系统使用的编码不同所致.Linux下使用的编码是utf8,而Windows使用的是gb18030. 解决方案: 在终端中,进入到 ...
- hdu 2112 (最短路+map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others) ...
- FLASH CC 2015 CANVAS 中 createjs 移除绑定事件
myBtn.addEventListener("click", function(evt) { // do stuff... evt.remove(); // removes th ...
- c++ 爬虫
这是一个简单的c++爬虫,效率并不是很高... #include<stdio.h> ],s2[]; void fun(int a, int b) { int i,ii; bool t1,t ...
- iOS - OC 术语表
1.术语表
- Redis主从配置详细过程
Redis的主从复制功能非常强大,一个master可以拥有多个slave,而一个slave又可以拥有多个slave,如此下去,形成了强大的多级服务器集群架构.下面楼主简单的进行一下配置. 1.上面安装 ...
- python多线程下载
# -*- coding=utf-8 -*- import sys import os import os.path import time import urllib.request, urllib ...
- 【Todo】单例模式各种实现方式及并发安全
Java 40道面试题不错:http://www.tuicool.com/articles/VRVFZb 其中有一道题目: 单例模式的线程安全性 老生常谈的问题了,首先要说的是单例模式的线程安全意味着 ...
- mysql 前缀索引
计算适合设置索引的长度,直到去重以后在一个固定值. 根据去重以后适合的长度设置索引. 计划查询