typescript node.js表示路由分离文件的最佳实践

时间:2016-05-11 15:49:56

标签: node.js express module typescript commonjs

在Node项目中使用Express以及Typescript什么是"最佳实践" for express.Router。

示例目录结构

|directory_name
  ---server.js
  |--node_modules
  |--routes
     ---index.ts
     |--admin
        ---admin.ts
     |--products
        ---products.ts
     |--authentication
        ---authentication.ts

所以在index.ts里面它会封装和管理所有的子路由器

    //admin.ts (nested inside of index.ts)
    import * as express from "express";

    export = (() => {
        
        let router = express.Router();
              
        router.get('/admin', (req, res) => {
            res.json({success: true});
        });
        
        return router;
    })();



    //admin.ts (nested inside of index.ts)
    import * as express from "express";

    export = (() => {
        
        let router = express.Router();
              
        router.get('/admin', (req, res) => {
            res.json({success: true});
        });
        
        return router;
    })();




最后我们会设置我们的server.js

    //index.ts (master file for express.Router)

    import * as express from "express";

    //import sub-routers
    import * as adminRouter from "./admin/admin";
    import * as productRouter from "./products/products";

    export = (() => {

      let router = express.Router();

      // mount express paths, any addition middleware can be added as well.
      // ex. router.use('/pathway', middleware_function, sub-router);

      router.use('/products', productRouter);
      router.use('/admin', adminRouter);

      //return for revealing module pattern
      return router;
    })(); //<--- this is where I don't understand something....

我的主要问题是,index.ts(masterRouter文件)和它的嵌套路由是IIFe&#39;

export =(function(){})();

这应该是为路由器编写打字稿模块的正确/最佳方式吗?

或者使用其他模式会更好,也许是利用模式

  

export function fnName(){} - export class cName {} - export default。

IIFe的原因是当我将它们导入另一个文件时,我不需要初始化模块,每种类型的路由器只会有1个实例。

1 个答案:

答案 0 :(得分:13)

答案

在NodeJS 中,每个文件都是一个模块。声明变量不会污染全局命名空间。因此,您不需要使用良好的旧<ol class="DataList_row"> <li class="Record_row"> <div class="Item_main"> <a href="#" class="arrow_box"> <div class="status"> <span class="Status_offline"><i class="fa fa-circle"></i></span> </div> </a> </div> </li> <li class="Record_row"> <div class="Item_main"> <a href="#" class="arrow_box"> <div class="status"> <span class="Status_online"><i class="fa fa-circle"></i></span> </div> </a> </div> </li> </ol> 技巧来正确定位变量(并防止全局污染/碰撞)。

你会写:

IIFE

有关模块的更多信息

https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

我的反应

  

我的主要问题是,index.ts(masterRouter文件)和IIFe import * as express from "express"; // import sub-routers import * as adminRouter from "./admin/admin"; import * as productRouter from "./products/products"; let router = express.Router(); // mount express paths, any addition middleware can be added as well. // ex. router.use('/pathway', middleware_function, sub-router); router.use('/products', productRouter); router.use('/admin', adminRouter); // Export the router export = router; 的嵌套路由应该是为路由器编写打字稿模块的正确/最佳方式

http://memesvault.com/wp-content/uploads/Hell-No-Meme-Gif-13.jpg