1.基本使用
var express=require('express');
var app=express();
app.use(express.static(__dirname+'/public'));//可以访问public目录下的文件,例如localhost:1234/about.html,about.html需放在public文件下
app.listen(1234,'localhost');
2.app.get方法
app.get('/',function (request,response,next) {//当路由为localhost:1234/时操作
console.log((request.query));//这里是获取地址后的参数:localhost:1234?name="oby"得到:{name:'oby'}
});
(1)传参
app.param('bookname',function(request,response,next,bookname){//这里可以对参数bookname进行处理如localhost:1234/list/js此时bookname就是js
console.log(bookname);
request.booklist=['book1','book2','book3'];//可以将数据存在request中
next();
})
app.get('/list/:bookname',function (request,response,next) {
response.end(request.booklist.join(','));//获取param中储存的数据
});
app.get('/list/:bookname/:id',function (request,response,next) {
response.end(request.booklist[request.params.id]);//也可以通过request.params.参数名来直接访问
});
});
get方法也可以接收一个正则表达式这里的例子是必须以mobile开头结尾的路由:localhost:1234/mobile
app.get(/^\/mobile\/$/,function (request,response,next) { response.end("mobile") });
(2)next()
app.get('/',function (request,response,next) {
console.log(1);
next('route');//如果有这句话就不会走console.log(2),直接到console.log(3)
},function (request,response,next) {
console.log(2);
next(); //若没有这句话就走不了console.log(3)一直停在这里
});
app.get('/',function (request,response,next) {
console.log(3); next();
},function (request,response,next) {
console.log(4); response.end("first")
});
(3)定向跳转,跳转到指定路由
app.get('/redirect',function (request,response,next) {
// body...
response.redirect('/test');
});
3.app.use方法
app.use(function (request,response,next) {//无论路径是什么,都返回这个函数操作
response.writeHead(200,{
"Content-type":'text-plain'
});
response.end("second");
});
可以利用request.url来判断路由,但是没有必要,有get方法简单
app.use(function (request,response,next) { if(request.url==='/'){ response.writeHead(200,{ "Content-type":'text-plain' }); response.end("first"); }else{ next(); } });
4.模板渲染
这里使用handlebars
var express=require('express');
var hbs=require('express-handlebars');
var app=express();
app.engine('hbs',hbs());//理解为注册模板
app.set('view engine','hbs');//设置默认模板渲染模板文件有后缀名可以省略这一句,即如果下面的‘render’写为‘render.hbs’这句话可以省略
app.set('views','templates');//默认是寻找名为views下的模板,这里设置可以改为寻在templates下的模板
app.get('/test',function(request,response,next){ response.locals={name:'jjjjjj'};//这里的数据默认传到2中,相当于2的response.render('render',{name:'jjjjjj'}) next();//注意此处一定要next },function(request,response,next){ response.render('render');//2 });
app.get('/test',function(request,response,next){ response.render('render',{name:'oby'});//直接在此处传参 });
这里的render.hbs为
<head> </head> <body> <h1>heandlebar{{name}}</h1> </body>
app.listen(1234,'localhost');
5.app.post方法
var express=require('express'); var hbs=require('express-handlebars'); var bodyParse=require('body-parser');//对post的body进行处理 var app=express(); app.engine('hbs',hbs()); app.get('/',function(request,response){//普通访问时的处理 response.render('form.hbs'); }) app.use(bodyParse.urlencoded({extend:true}));//通过这样才能得出body的数据 app.post('/',function(request,response){//按了submit的处理 console.log(request.body.name) console.log(request.body.password) }) app.listen(1234,'localhost')
form.hbs
<form method="post"> <input type="text" name="name"> <input type="text" name="password"> <input type="submit" name="submit"> </form>
6session
var express=require('express'); var session=require('express-session'); var app=express(); 当前服务器内有效 app.use(session({secret:'oby'})); app.get('/',function(request,response,next){ response.send('userId'+request.session.userId);//当不传参时获取上次的userid }); app.get('/:id',function(request,response,next){ request.session.userId=request.params.id;//通过session存储数据 response.send('hello'); }); app.listen(1234,'localhost');
浙公网安备 33010602011771号