express-autoload-router源碼分析

模塊介紹

express-autoload-router模塊用於自動加載並註冊路由。node

模塊使用

基本用法

基本使用步驟以下。git

安裝模塊

$ npm install express-autoload-router

構建路由程序

將路由程序文件放在專門的目錄app/controllers下面。格式上有兩點須要注意:github

  1. 路由程序文件名稱必須爲xxx_controller.js
  2. 路由程序中的action函數/對象名稱必須爲yyyAction
$ cat app/controllers/user_controller.js
/**
 * 對象方式,多個路由放在一塊兒
 */
module.exports = {
    listAction: {
        method: ["GET"],
        middlewares: [],
        handler: function(req, res) {
            return res.send("user list action");
        }
    },
    getAction: {
        method: ["GET"],
        middlewares: [],
        handler: function(req, res) {
            return res.send("user get action");
        }
    }
};

在主程序中引入並加載路由

$ cat app.js
const express = require("express");
const path = require('path');
const loadRouter = require('express-autoload-router');

const app = express();

loadRouter(app, "/demo", path.join(__dirname, "app/controllers"));

app.listen(3000, function() {
    console.log("Listening on port 3000!");
});

其中,loadRouter()函數的第二個參數指定一級路由,第二個參數指定路由程序文件所在的目錄。express

該函數會調用app.METHOD()(METHOD在這裏替換爲get、post等HTTP方法)註冊相應的路由,路由URI爲:/demo/xxx/yyynpm

使用示例

$ curl -X GET http://localhost:3000/demo/user/list
user list action

路由程序文件的寫法

路由程序文件中的yyyAction能夠寫成函數,也能夠寫成對象,二者是等價的。app

使用函數

基本寫法以下:curl

// 普通路由,訪問方式:/demo/product/list
module.exports.listAction = function(req, res) {
    return res.send("product list action");
};

因爲在node.js當中,module.exportsexports等價,因此也能夠寫成:函數

// module.exports和exports等價
exports.getAction = function(req, res) {
    return res.send("product get action");
}

另外,函數也能夠簡化:源碼分析

// 簡化函數:function 改爲 =>
exports.simpleAction = (req, res) => {
    return res.send("product simple action");
};

函數再簡化一點:post

// 更簡化函數:function 改爲 =>,省略大括號
// URL使用大小寫都可:/demo/product/moreSimple 或 /demo/product/moresimple
exports.moreSimpleAction = (req, res) => res.send("product moreSimple action");

使用對象

基本寫法:

module.exports.buyAction = {
    method: ["GET", "POST"],
    middlewares: [],
    handler: function(req, res) {
        return res.send("product buy action");
    }
};

等價寫法:

// handler的另外一種寫法
exports.sellAction = {
    method: ["GET", "POST"],
    middlewares: [],
    handler(req, res) {
        return res.send("product sell action");
    }
};

注意事項

indexAction的處理

indexAction中的index不會做爲組成路由的一部分。好比,對於路由文件product_controller.js,有:

// 默認路由,訪問方式:/demo/product
module.exports.indexAction = function(req, res) {
    return res.send("product index action");
};

// 普通路由,訪問方式:/demo/product/list
module.exports.listAction = function(req, res) {
    return res.send("product list action");
};

其中,listAction對應的路由爲/demo/product/list,而indexAction對應的路由爲/demo/product

路由子目錄(子路由)

假設controllers目錄下有一個子目錄subdir,其中有一個路由程序文件subroute_controller.js,以下:

$ cat app/controllers/subdir
module.exports.listAction = function(req, res) {
    return res.send("subdir/subroute list action");
};

listAction對應的路由爲/demo/subdir/subroute/list。因而可知,子目錄有會做爲路由的一部分。

源碼分析

參考資料

相關文章
相關標籤/搜索