express-2-中间件



/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端口")
}
})

相关文章:

  • 2022-12-23
  • 2022-01-08
  • 2021-09-20
  • 2021-06-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
猜你喜欢
  • 2021-08-10
  • 2022-01-11
  • 2021-10-11
  • 2022-12-23
  • 2022-03-06
相关资源
相似解决方案