码迷,mamicode.com
首页 > 其他好文 > 详细

中间件

时间:2018-01-15 22:26:06      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:nbsp   重要   127.0.0.1   stat   sorry   var   func   console   top   

如果我的的getpost回调函数中,没有next参数,那么就匹配上第一个路由,就不会往下匹配了。

如果想往下匹配的话,那么需要写next()

1 app.get("/",function(req,res,next){

2     console.log("1");

3     next();

4 });

6 app.get("/",function(req,res){

7     console.log("2");

8 });

 

下面两个路由,感觉没有关系:

1 app.get("/:username/:id",function(req,res){

2     console.log("1");

3     res.send("用户信息" + req.params.username);

4 });

6 app.get("/admin/login",function(req,res){

7     console.log("2");

8     res.send("管理员登录");

9 });

但是实际上冲突了,因为admin可以当做用户名 login可以当做id

 

解决方法1:交换位置。 也就是说,express中所有的路由(中间件)的顺序至关重要。

匹配上第一个,就不会往下匹配了。 具体的往上写,抽象的往下写。

1 app.get("/admin/login",function(req,res){

2     console.log("2");

3     res.send("管理员登录");

4 });

6 app.get("/:username/:id",function(req,res){

7     console.log("1");

8     res.send("用户信息" + req.params.username);

9 });

 

解决方法2

1 app.get("/:username/:id",function(req,res,next){

2     var username = req.params.username;

3     //检索数据库,如果username不存在,那么next()

4     if(检索数据库){

5         console.log("1");

6         res.send("用户信息");

7     }else{

8         next();

9     }

10 });

11 

12 app.get("/admin/login",function(req,res){

13     console.log("2");

14     res.send("管理员登录");

15 });

 

路由getpost这些东西,就是中间件,中间件讲究顺序,匹配上第一个之后,就不会往后匹配了。next函数才能够继续往后匹配。

 

app.use()也是一个中间件。与getpost不同的是,他的网址不是精确匹配的。而是能够有小文件夹拓展的。

比如网址:  http://127.0.0.1:3000/admin/aa/bb/cc/dd

1 app.use("/admin",function(req,res){

2     res.write(req.originalUrl + "\n");   //    /admin/aa/bb/cc/dd

3     res.write(req.baseUrl + "\n");  //   /admin

4     res.write(req.path + "\n");   //    /aa/bb/cc/dd

5     res.end("你好");

6 });

 

如果写一个/

1 //当你不写路径的时候,实际上就相当于"/",就是所有网址

2 app.use(function(req,res,next){

3     console.log(new Date());

4     next();

5 });

 

app.use()就给了我们增加一些特定功能的便利场所。

实际上app.use()的东西,基本上都从第三方能得到。

 

 

● 大多数情况下,渲染内容用res.render(),将会根据views中的模板文件进行渲染。如果不想使用views文件夹,想自己设置文件夹名字,那么app.set("views","aaaa");

● 如果想写一个快速测试页,当然可以使用res.send()。这个函数将根据内容,自动帮我们设置了Content-Type头部和200状态码。send()只能用一次,和end一样。和end不一样在哪里?能够自动设置MIME类型。

● 如果想使用不同的状态码,可以:

res.status(404).send(‘Sorry, we cannot find that!‘);

● 如果想使用不同的Content-Type,可以:

res.set(‘Content-Type‘, ‘text/html‘);

中间件

标签:nbsp   重要   127.0.0.1   stat   sorry   var   func   console   top   

原文地址:https://www.cnblogs.com/junnliu/p/8289559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!