express-2-中间件

来源:互联网 发布:sql 数据默认值 编辑:程序博客网 时间:2024/06/08 14:48



/public/index.html
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Title</titel>
</head>
<body>
<p>中间件</p>
</body>
/app.js
var http=require("http");
var express = require("express");
var app = express();

//自定义中间件
//中间件是按顺序执行,如果没有用到next,则会停止
app.use("/",function(req,res,next){
console.log("进入自定义中间件");
next();
});

//挂载中间件 ,第一个参数是指定路径,默认值是“/”
//传入public,能访问到静态页面,express.static为express自带中间件
app.use("/",express.static(__dirname+"/public"))

app.all("/index",function(req,res){
console.log("hello server")
res.send("hello browser!");
res.end();
})
http.createServer(app).listen(3000,function(err){
if(err){
console.log("服务器错误")
}else{
console.log("服务器启动成功,监听3000端口")
}
})

原创粉丝点击