【问题标题】:Does index.js slowdown Nodejs when loading加载时 index.js 是否会减慢 Nodejs
【发布时间】:2021-08-08 02:44:27
【问题描述】:

我的 NodeJS 应用程序需要很长时间(30 秒)来加载和重新加载使用 nodemon 进行的每次更改。我有 15 个实用程序和 35 条路线。我有一个 utils/index.js 和 routes/index.js 文件,用于导入和导出这些文件夹中的所有内容。

我怀疑 index.js 文件可能会导致应用一次加载和导入/导出所有文件,这可能会在启动时以某种方式减慢它。

问题:拥有 index.js 会减慢 NodeJS 的速度吗?我加载路线的方式效率低吗?或者启动这种大小的应用需要 30 秒正常吗?

routes/route1.js

export const route1 = (app) => {
  app.get('/route1', (req, res, next) => { ... }
}

路由/index.js

// I'm using latest ES6 imports
import { route1 } from './route1';
// ...
import { route35 } from './route35';

export const route = (app) => {
  route1(app);
  // ...
  route35(app);
}

App.js

// ...
import { routes } from 'routes/index';
routes(app);

【问题讨论】:

    标签: node.js performance import routes


    【解决方案1】:

    不要将每个路由都放在一个文件中,而是将它们放在一个文件中的一个类别中,这样加载时间就会下降并使用https://expressjs.com/en/guide/routing.html#express-router 不要导出函数,只需导出路由器 obj,这样您就不必执行该函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多