一、安裝express cnpm || npm install express --save ; 1. Hello World 2.express路由 3.靜態資源 app.user(express.statice('public')); 二、express的中間件 1.應用級中間件 2.路由級中間 ...
一、安裝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