一、概念
app.use([path],function)
注:中间件写时,要在当前的路由之前写
二、分类
1.全局应用级中间件
App.js:
var express=require("express");
var app=new express();
app.set("view engine","ejs");
app.use(function(req,res,next){
console.log("我是应用级中间件");
next();
});
app.get("/",function(req,res){
res.render("login");
});
app.get("/index",function(req,res){
res.render("index",req.query);
});
app.listen(8100);
login.ejs:
<body>
<form method="GET" action="/index">
<ul>
<li>账号:<input type="text" name="id"></li>
<li>密码:<input type="text" name="pwd"></li>
<li><input type="submit" value="登录"></li>
</ul>
</form>
</body>
index.ejs:
<body>
<div>系统主界面</div>
<div>账号------<%=id%></div>
<div>密码------<%=pwd%></div>
</body>
应用场景:缓存值
2.路由级中间件(匹配当前路由,在执行当前路由之前进行的操作)
App.js:
var express=require("express");
var app=new express();
var user=require("./user");
app.set("view engine","ejs");
app.use("/index",function(req,res,next){
var u=req.query;
if(u.id==user.id && u.pwd==user.pwd){
next();
}else{
res.render("login",{
isshow:true
});
}
});
app.get("/",function(req,res){
res.render("login",{
isshow:false
});
});
app.get("/index",function(req,res){
res.render("index",req.query);
});
app.listen(8100);
login.ejs:
<body>
<% if(isshow){%>
<div>账号或密码错误!</div>
<%}%>
<form method="GET" action="/index">
<ul>
<li>账号:<input type="text" name="id"></li>
<li>密码:<input type="text" name="pwd"></li>
<li><input type="submit" value="登录"></li>
</ul>
</form>
</body>
index.ejs:
<body>
<div>系统主界面</div>
<div>账号------<%=id%></div>
<div>密码------<%=pwd%></div>
</body>
user.js:
module.exports={
id:12345,
pwd:121212
}
3.错误级中间件
后期维护时,错误日志的打印:
var express=require("express");
var ejs=require("ejs");
var fs=require("fs");
var app=new express();
app.set("view engine","ejs");
app.get("/regest",function(req,res,next){
fs.readFile("./data.txt",function(err,data){
if(err){
next(err);
}
res.send(data);
});
});
//错误级中间件 在项目里出现错误时执行的中间件
app.use(function(err,req,res,next){
console.error(err.stack); //报出错误的状态码及错误
fs.appendFile("./error/error.txt",err.stack+"\n",function(error){
if(err){
next(error);
return;
}
console.log("追加成功!");
});
console.log("我是错误级中间件");
res.status(404).render("err");
});
app.listen(8100);
当前路由不存在,渲染404页面出去:
app.use(function(req,res,next){
console.log("我是错误级中间件2");
res.status(404).render("error");
});
4.第三方中间件
(1)body-parser
cnpm install body-parser --save
var bodyParser=require("body-parser");
//解析application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended:false}));
//解析application/json
app.use(bodyParser.json());
//post路由接收传值
app.post("/userlogin",function(req,res,next){
console.log(req.body);
res.render("success");
});
App.js:
var express=require("express");
var ejs=require("ejs");
var app=new express();
var bodyParser=require("body-parser");
//配置express框架,使用body-parser
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.set("view engine","ejs");
app.use(express.static("static"));
app.get("/",function(req,res){
res.render("index",{
list:"首页"
},function(err,str){
res.send(str);
});
});
//post路由接收传值
app.post("/userlogin",function(req,res,next){
console.log(req.body);
res.render("success");
});
app.listen(8100);
index.ejs:
<body>
<div>我是<%=list%></div>
<form method="POST" action="/userlogin">
<ul>
<li>姓名:<input type="text" name="username"></li>
<li><input type="submit"></li>
</ul>
</form>
</body>
success.ejs:
<body>
<div>表单提交成功!</div>
</body>
(2)cookie-parser(写入缓存的中间件)
cnpm install cookie-parser --save
var cookieParser=require("cookie-parser");
app.use(cookieParser());
var express=require("express");
var ejs=require("ejs");
var app=new express();
app.set("view engine","ejs");
app.use(express.static("static"));
var cookieParser=require("cookie-parser");
app.use(cookieParser());
app.get("/setcookie",function(req,res){
//设置cookie、有效期、maxAge:时间
res.cookie("user","kun",{maxAge:10000});
res.send("设置cookie成功!");
});
app.get("/getcookie",function(req,res){
//获取cookie
var user=req.cookies.user; //未加密的获取方式
console.log(user);
res.send("获取cookie成功!");
});
app.listen(8100);
var express=require("express");
var ejs=require("ejs");
var app=new express();
app.set("view engine","ejs");
app.use(express.static("static"));
var cookieParser=require("cookie-parser");
app.use(cookieParser("123456"));
app.get("/setcookie",function(req,res){
//设置cookie、有效期、maxAge:时间、signed:设置签名
res.cookie("user","kun",{maxAge:10000,signed:true});
res.send("设置cookie成功!");
});
app.get("/getcookie",function(req,res){
//获取cookie
var user=req.signedCookies.user; //加密的获取方式
console.log(user);
res.send("获取cookie成功!");
});
app.listen(8100);