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

And*_*rei 7 module commonjs node.js express typescript

在Node项目中使用Express和Typescript,这将是express.Router的"最佳实践".

示例目录结构

|directory_name
  ---server.js
  |--node_modules
  |--routes
     ---index.ts
     |--admin
        ---admin.ts
     |--products
        ---products.ts
     |--authentication
        ---authentication.ts
Run Code Online (Sandbox Code Playgroud)

所以在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;
    })();
Run Code Online (Sandbox Code Playgroud)

    //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....
Run Code Online (Sandbox Code Playgroud)

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

    //the usual node setup
    //import * as *** http, body-parser, morgan, mongoose, express <-- psudocode

    import * as masterRouter from './routes/index'

    var app = express();
    //set-up all app.use()

    app.use('/api', masterRouter);

    http.createServer(app).listen(8080, () => {
          console.log('listening on port 8080')
        };
Run Code Online (Sandbox Code Playgroud)

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

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

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

或者使用另一种模式会更好,也许是利用模式的模式

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

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

当json包含没有键的数组时,如何检查swiftyJSON中是否存在键

我知道swiftyJSON方法exists()但它似乎并不总是如他们所说.在下面这种情况下如何获得正确的结果?我无法更改JSON结构,因为我通过客户端的API获取此信息.

|directory_name
  ---server.js
  |--node_modules
  |--routes
     ---index.ts
     |--admin
        ---admin.ts
     |--products
        ---products.ts
     |--authentication
        ---authentication.ts
Run Code Online (Sandbox Code Playgroud)

输出:

    //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;
    })();
Run Code Online (Sandbox Code Playgroud)

不应该打印,因为someKey不存在.但有时候这个密钥来自客户端的API,我需要找出它是否存在.

bas*_*rat 16

回答

在NodeJS中,每个文件都是一个模块.声明变量不会污染全局命名空间.所以你不需要使用好的老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;
Run Code Online (Sandbox Code Playgroud)

更多关于模块

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

我的反应

我的主要问题是,index.ts(masterRouter文件)和IIFe的嵌套路由export = (function(){})();应该是为路由器编写typescript模块的正确/最佳方式

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


Copyright Info

© Copyright 2013-2021 admin@qa.1r1g.com

如未特别说明,本网站的内容使用如下协议:
Creative Commons Atution-NonCommercial-ShareAlike 4.0 International license
.

用以下方式浏览
回到顶部