一、安装express
cnpm || npm install express --save ;
1. Hello World
var express = require('express'); var app = express(); app.get('/',function(req,res){ res.send('hello world'); }); var server = app.listen('3000',function(){ var host = server.address().address; var port = server.address().port; console.log('server start localhost',host,port); })
2.express路由
var express = require('express'); var app = express(); //http://localhost:3000/ app.get('/',function(req,res){ res.send('hello world'); }); //http://localhost:3000/user.html app.get('/user.html',function(req,res){ res.sendFile(__dirname + '/' + 'user.html'); }); //http://localhost:3000/index.html app.get('/index.html',function(req,res){ res.sendFile(__dirname + '/' + 'index.html') }); /* *其实每个路由对于着每个方法,把 req() => 请求的api 看懂 * * 把 res(); => 的api 看会。就知道怎么和服务端交换啦。 * * 当客户端发送AJAX请求的时候,其实就是方法的每个方法。 * */ var server = app.listen('3000',function(){ var host = server.address().address; var port = server.address().port; console.log('server start localhost',host,port); })
3.静态资源 app.user(express.statice('public'));
var express = require('express'); var app = express(); //设置静态资源的位置 app.use(express.static('public')); //http://localhost:3000/img/1.jpg //public/img/1.jpg var server = app.listen('3000',function(){ var host = server.address().address; var port = server.address().port; console.log('server start localhost',host,port); })
二、express的中间件
1.应用级中间件
var express = require('express'); var app = express(); // 没有挂载路径的中间件,应用的每个请求都会执行该中间件 app.use(function(req,res,next){ console.log('应用级中间件'); console.log('time' + Date.now()); next(); }) app.get('/',function(req,res){ res.send('我是 根路径') }); // 挂载至 /user/:id 的中间件,任何指向 /user/:id 的请求都会执行它 app.get('/user/:id',function(req,res,next){ console.log('我是 user路由上面的子中间件'); next(); }); app.get('/user/:id',function(req,res){ var id = req.params.id; res.send(id); }); app.get('/method/:id',function(req,res,next){ console.log('methos路径的参数',req.params); next(); },function(req,res,next){ console.log('methods路径的第二个参数',req.method) next(); }) app.get('/method/:id',function(req,res){ var path = req.route; res.send(path); }); var server = app.listen('3000',function(){ var host = server.address().address; var port = server.address().port; console.log('server start localhost',host,port); })
2.路由级中间件 express.Router();
var express = require('express'); var router = express.Router(); var app = express(); router.use(function(req,res,next){ console.log(req.path); next() }); router.get('/user/:id',function(req,res,next){ console.log(req.originalUrl); next(); }) router.get('/user/:id',function(req,res){ res.send(req.params); }) app.use('/',router) var server = app.listen('8080',function(){ console.log('start') });
3.错误级中间件
var express = require('express'); var app = express(); app.use(function(error,req,res,next){ res.status('500').send('something error'); }) var server = app.listen('3000',function(){ console.log('server start'); })
4.静态资源中间件 express.static();
三、模板引擎
1.安装jade模板引擎,cnpm || npm install jade --save
var express = require('express'); var app = express(); app.set('views','./views'); app.set('view engine','jade'); app.get('/',function(req,res){ res.render('index',{title:'jade',message:'模板引擎'}) }); var server = app.listen('3000',function(req,res){ console.log('server start'); })
2.view 下面的index.jade
html head title!= title body h1!= message