1. 程式人生 > >Node.js —— 使用express模組建立靜態web伺服器及其路由

Node.js —— 使用express模組建立靜態web伺服器及其路由

1、使用express建立靜態web伺服器

首先使用命令列在當前路徑建立目錄public(可自定義),並在目錄下建立檔案test.txt(可自定義)

$ mkdir public
$ vim test.txt //(檔案內容自定義)

在 node.js 檔案中指定靜態目錄(./public是靜態目錄)

app.use(express.static("./public"));

通過命令列訪問靜態目錄下的靜態檔案(“/test.txt”是靜態目錄下的檔案的相對路徑)

$ curl http://localhost:18001/test.txt 

2、路由的建立方法

路由:將不同的請求,分配給相應的處理函式

(1)path 方法,即直接填入路由的相對路徑

app.get('/add',function(req,res){
    res.end('path /add\n');
})

命令列下執行

$ curl http://localhost:18001/add

(2)路由的Router方法,用於建立在一個路由下的多個子路由

var Router=express.Router();

Router.get('/add',function(req,res){
    res.end('Router /add\n');
});// 建立子路由add

Router.get('/list',function
(req,res){
res.end('Router /list\n'); });// 建立子路由list app.use('/post',Router);// post是主路由

命令列下執行

$ curl http://localhost:18001/post/add)
$ curl http://localhost:18001/post/list)

(3)路由的route方式,用於建立一個路由在不同方法下的不同處理

app.route('/article')
    .get(function(req,res){
        res.end("route /article get\n");
    })// 對get方法的處理
.post(function(req,res){ res.end("route /article post\n"); });// 對post方法的處理

命令列下執行(-X用於指定方法,若不指定,預設為GET)

$ curl -X POST http://localhost:18001/article
$ curl -X GET http://localhost:18001/article 

3、路由引數的使用

建立路由引數

語法:app.param( name,function ),name是路由引數的名字,function是路由引數的處理函式

app.param('newsId',function(req,res,next,newsId){
    req.newsId=newsId;
    next();
});

使用路由引數

app.get('/news/:newsId',function(req,res){
    res.end("newsId:"+req.newsId+"\n");
});

命令列下輸入

$ curl http://localhost:18001/news/123 

完整程式碼:

var express=require("express");
var app=express();

// 使用express建立靜態伺服器
app.use(express.static("./public"));


// 路由的path方法,即直接填入檔案路徑
app.get('/add',function(req,res){
    res.end('path /add\n');
})

// 路由的Router方法
var Router=express.Router();
Router.get('/add',function(req,res){
    res.end('Router /add\n');
});
Router.get('/list',function(req,res){
    res.end('Router /list\n');
});
app.use('/post',Router);

// 路由的route方式
app.route('/article')
    .get(function(req,res){
        res.end("route /article get\n");
    })
    .post(function(req,res){
        res.end("route /article post\n");
    });

// 路由引數
app.param('newsId',function(req,res,next,newsId){
    req.newsId=newsId;
    next();
});
app.get('/news/:newsId',function(req,res){
    res.end("newsId:"+req.newsId+"\n");

});

app.listen(18001,function afterListen(){
    console.log("express running on http://localhost:18001");
});

4、使用express模板

1、安裝express-generator

npm install -g express-generator

2、在當前資料夾中,建立 express 專案

express [專案名稱]

3、進入 express 專案,並安裝相關依賴包

cd [express專案的路徑]
npm install

4、執行express專案(express的執行指令碼是bin/www)

node bin/www

5、關於中介軟體

1、connect:node.js的中介軟體框架
2、分層處理
3、每層實現一個功能
4、預處理中介軟體
5、後置處理中介軟體

// 後置處理中介軟體
ResellerSchema.post('save',function (next) {
    console.log('this is ResellerSchema post save middleware');
    next();
});
// 預處理中介軟體
ResellerSchema.pre('save',function (next) {
    console.log('this is ResellerSchema pre save middleware');
    next();
});