简体   繁体   English

Node.js Express.js使NPM软件包在整个项目中可用

[英]Nodejs expressjs make npm package available throughout the project

I've this folder structure. 我有这个文件夹结构。

/app.js
/src
 /routes
 /controllers

In routes folder, I've bunch of js files. 在路线文件夹中,我有一堆js文件。 All of these files need to require passport js package like this 所有这些文件都需要像这样的护照js包

const passport = require('passport');

Instead of doing this, can I require the package in one place (Most probably in app.js) and somehow pass that to each and every file in the routes folder instead of requiring it on every file. 除了执行此操作外,我是否可以将程序包放在一个位置(大多数可能在app.js中),然后以某种方式将其传递到routes文件夹中的每个文件,而不是在每个文件中都需要它。

There may be a passport/Express-specific solution (eg, installing passport once as middleware), but answering the question about modules in general: 可能存在特定于通行证/快递的解决方案(例如,将通行证作为中间件安装一次),但通常会回答有关模块的问题:

Requiring a module in a module that uses it is standard practice and clearly expresses the dependencies between modules, so it's not usually something you want to avoid doing. 在使用该模块的模块中要求模块是一种标准做法,可以清楚地表达模块之间的依赖关系,因此通常不必避免这样做。

Instead of doing this, can I require the package in one place (Most probably in app.js) and somehow pass that to each and every file in the routes folder instead of requiring it on every file. 除了执行此操作外,我是否可以将程序包放在一个位置(大多数可能在app.js中),然后以某种方式将其传递到routes文件夹中的每个文件,而不是在每个文件中都需要它。

You have a couple of options: 您有两种选择:

  • If all of those files have other things they're also all importing, you can create a rollup module that requires all of those things and then makes them available as exports. 如果所有这些文件还都具有其他导入功能,则可以创建一个需要所有这些内容的汇总模块,然后将其用作导出。 Then your files would do: 然后您的文件将执行以下操作:

     const {passport, anotherThing, yetAnotherThing} = require("./the-rollup-module"); 

    instead of 代替

     const passport = require("passport"); const anotherThing = require("another-thing"); const yetAnotherThing = require("yet-another-thing"); 

    The rollup would look like this: 汇总看起来像这样:

     module.exports.passport = require("passport"); module.exports.anotherThing = require("another-thing"); module.exports.yetAnotherThing = require("yet-another-thing"); 
  • (I don't recommend this.) You can make it a global by putting this in your entry script: (我不建议这样做。)可以通过将其放入输入脚本中来使其成为全局变量:

     global.passport = require("passport"); 

    That exposes passport as a global variable, so your modules could just use passport without require ing it. 暴露passport作为一个全局变量,所以你的模块可以只使用passport而不require荷兰国际集团它。 (The default global variable is a reference to the global object, like window on browsers, so any property you create on it becomes a global variable.) (默认的global变量是对全局对象的引用,就像浏览器上的window一样,因此您在其上创建的任何属性都将成为全局变量。)

    I don't recommend it because then the dependencies between your modules are no longer clearly defined. 我不建议这样做,因为这样就不再明确定义模块之间的依赖关系。

No matter how often you require() a module, it will be loaded just once. 无论您require()一个模块,它都只会被加载一次。 There is nothing wrong with requiring one module in multiple files, actually this is basically the way the module system is designed to work. 在多个文件中需要一个模块没有错,实际上,这基本上是模块系统设计为工作的方式。

If the files in the routes folder are not using the express router you can rather or else export the function which accepts passport and app objects like 如果路由文件夹中的文件未使用快速路由器,您可以选择导出导出接受护照和应用程序对象的功能,例如

module.exports = function(app, passport) {
  app.get('/', (req, res) => {
      res.json('some route');
  });
}

Then you will be requiring the passport only once in the app.js/server.js and passing the same object to every route 然后,您只需要在app.js / server.js中一次使用护照,并将同一对象传递到每条路线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM