//express 3中间件 依赖于connect
//静态文件处理中间件
app.use(express.static(__dirname+"/public"));
//日志处理中间件
//short 打印基本信息,打印默认信息去掉short,也可以自定义日志信息
app.use(express.logger("short"));
//post方式提交时请求数据解析
//console.log(req.body);
app.use(express.bodyParser());
//解析get方式提交的数据
//console.log(req,query());
app.use(express.query());
//cookie解析
app.use(express.cookieParse());
//启用sesssion管理客户状态
//一定要写在cookie解析下面
app.use(express.session({
secret:"keyboard cat",
key:"sid";
cookie:{secure:true}
}));
//网站图标处理中间件,写图标路径
app.use(express.favicon());
//学习网站 http://blog.fens.me/nodejs-connect/
express4中间件需要自己安装
到项目路径下
npm install body-parser
npm install cookie-parser
npm install cookie-session
npm install errorhandler
npm install express
npm install express -session,
上面需要require导入
var bodyParser = require("body-parser");
var multer = require("multer");
var cookieParser = require("cookie-parser");
var cookieSession = require("cookie-session");
var errorHandler = require("errorhandler");
//静态文件处理中间件
app.use(express.static(__dirname.substring(0,__dirname.lastIndexOf("/")+“/public”)));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.jsion());
app.use(cookieParse());
app.use(cookieSession({
secret:"keyboard cat",
key:"sid";
cookie:{
maxAge:1000*60*2,
domain:"localhost"
secure:true
}
}));
app.use(errorHandler({log:true}));
//学习网站:http://www.expressjs.com.cn/resources/middleware.html