原文地址:http://blog.sina.com.cn/s/blog_ad0672d60101l2lx.html
1.express組織結(jié)構(gòu)
app demo
|---node_modules------用于安裝本地模塊。
|---public------------用于存放用戶可以下載到的文件,比如圖片、腳本、樣式表等。
|---routes------------用于存放路由文件。
|---views-------------用于存放網(wǎng)頁的模板。
|---app.js------------應(yīng)用程序的啟動(dòng)腳本。
|---package.json------項(xiàng)目的配置文件。
2..創(chuàng)建express服務(wù)器
//app.js文件
var express = require('express');
var app = express();
//指定更目錄顯示的內(nèi)容
app.get('/', function(req, res){
res.send('Hello World');
});
//指定監(jiān)聽端口
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});
運(yùn)行nodejs應(yīng)用程序
/>node app.js
3.中間件
中間件(middleware)就是處理HTTP請(qǐng)求的函數(shù).
當(dāng)一個(gè)HTTP請(qǐng)求進(jìn)入服務(wù)器,服務(wù)器實(shí)例會(huì)調(diào)用第一個(gè)中間件,完成后根據(jù)設(shè)置,決定是否再調(diào)用下一個(gè)中間件.
中間件的參數(shù)為:
.四個(gè)的時(shí)候---第一個(gè)為錯(cuò)誤處理,第二個(gè)為客戶請(qǐng)求request,第三個(gè)為服務(wù)器響應(yīng)respond,第四個(gè)為next中間件. 如function(error, request, response, next){}
.三個(gè)的時(shí)候---第一個(gè)客戶請(qǐng)求request,第二個(gè)為服務(wù)器響應(yīng)respond,第三個(gè)為next中間件. 如function(request, response, next){}
.兩個(gè)的時(shí)候---第一個(gè)客戶請(qǐng)求request,第二個(gè)為服務(wù)器響應(yīng)respondfunction. 如function(request, response){}
4.使用中間件use
use是express調(diào)用中間件的方法,它返回一個(gè)函數(shù).
app.use(function(request, response) {
response.writeHead(200, { "Content-Type": "text/plain" });
response.end("Hello world!\n");
});
5.錯(cuò)誤內(nèi)容顯示
app.use(express.bodyParser());//使用body參數(shù)
app.use(express.methodOverride());//使用函數(shù)覆蓋
app.use(app.router);//使用路由
app.use(function(err, req, res, next){
console.error(err.stack);
res.send(500, 'Something broke!');
});//錯(cuò)誤內(nèi)容顯示
6.路由
express路由的方式有多種,這里舉例常用的幾種:
.app.use('/', middleware);//get/post時(shí),對(duì)于路徑/的處理
.app.get("/", middleware);//http中g(shù)et時(shí),對(duì)于路徑/的處理
.app.post("/", middleware);//http中post時(shí),對(duì)于路徑/的處理
.app.put("/", middleware);//http中put時(shí),對(duì)于路徑/的處理
.app.delete("/", middleware);//http中delete時(shí),對(duì)于路徑/的處理
7.路徑通配符*
.*表示所有路徑
app.get("*", function(request, response) {
response.end("404!");
});//所有路徑都返回404
.:捕獲路徑內(nèi)容
app.get("/hello/:who", function(req, res) {
res.end("Hello, " + req.params.who + ".");
});//如"/hello/alice”網(wǎng)址,網(wǎng)址中的alice將被捕獲,作為req.params.who屬性的值
8.設(shè)置環(huán)境變量set
set用于指定變量的值.
app.set("view engine", "ejs");//使用ejs作為模版
9.response對(duì)象方法
.重定向redirect
response.redirect("/hello/anime");//重定向到/hello/anime
.發(fā)送文件sendFile
response.sendFile("/path/to/anime.mp4");
.渲染網(wǎng)頁模板render,即把變換的內(nèi)容加載到網(wǎng)頁.
response.render("index", { message: "Hello World" });//將message變量傳入index模板,值為"Hello World"渲染成HTML網(wǎng)頁
10.requst對(duì)象方法
.獲取客戶ip地址:request.ip
.獲取上傳的文件:request.files
11.啟動(dòng)腳本package.json
package.json用于指定app信息,nodejs版本號(hào)和其他組件的依賴關(guān)系
{
"name": "demo",
"description": "My First Express App",
"version": "0.0.1",
"dependencies": {
"express": "3.x"
}
}
12.app入口app.js
app.js主要包含http的創(chuàng)建,基本路由,監(jiān)聽端口號(hào)
13.動(dòng)態(tài)網(wǎng)頁模板views
views文件夾,用于存放所有的放網(wǎng)頁模板.
//app.js
app.get('/', function(req, res) {
res.render('index',{title:"最近文章"});
});