app.get和app.post
1.请求的方法必须是get/post
2.请求的路径的pathname必须等于(====)路径
app.use
1.在进行路由匹配的时候不限定方法,什么请求方法都可以
2.请求路径中的第一部分只要与/index相等即可,并不要求路径完全匹配
在请求静态资源时
//当以/public/开头时,去./public/目录中找对应的资源 app.use('/public/',express.static('./public/')); //在浏览器中加xxx/public/文件 就可以访问了 //如果第一个参数不传 app.use(express.static('./public')); //在浏览器中加xxx/public/文件 不能访问,要去掉/public,在浏览器中加xxx/文件 //可以使用其他名字代替/public, app.use('/a/',express.static('./public/')); //在浏览器中加xxx/a/文件 就可以访问了
app.all
1.不限定请求方法
2.请求路径的pathname完全匹配
正则
如果想限定请求方式,又要pathname不用完全匹配,可以考虑正则表达式
var express=require('express'); var app=express(); //通过中间件监听指定路由请求 app.get('/',function(req,res){ res.send('Index'); }); app.post('/add',function(req,res){ res.send('add'); }); app.use('/index',function(req,res){ res.send('hhh,|> v <|'); });
app.get(/^/submit(/.+)*$/,function(req,res){
res.send('submit');
});
app.all('/item',function(req,res){
res.send('item');
});
app.listen(9092,function(){ console.log('http://localhost:9092'); })
app.get
app.use
正则
可按实际需要选择使用