本文介绍了使用需求而不导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var express =要求( '表示'); 
var bodyParser = require('body-parser');
var logger = require('morgan');

var app = express();

require('./ init / db');
require('./ init / cache'); //引入Redis

//定义路由
var userRoutes = require('./ routes / user');

module.exports = app;

我不明白的位是以这种方式使用的需要这是它带来的文件:

  // db.js 
var mongoose = require('mongoose') ;
var dbURI =< theURI> ;;

mongoose.connect(dbURI);

//连接事件
mongoose.connection.on('connected',function(){
console.log('Mongoose connected successfully');
} );

与我的Redis连接是一样的:

  // cache.js 
var redis = require(redis);

var redisClient = redis.createClient(process.env.CACHE_PORT,process.env.CACHE_URL);
redisClient.auth(process.env.CACHE_PASS);

redisClient.on(ready,function(){
console.log(Cache is connected);
});

但是您可以看到没有 module.exports 在$ code> db.js 或 cache.js 文件中的任何地方!当我google这个了解它是如何工作的,例子总是谈论 module.exports require 在一起。



问题


  1. 有人可以解释需求当你自己使用这样的作品时?


  2. 如何使缓存/ Redis连接可用,以便可以在我的 userRoutes 文件使用如下: var userRoutes = require('./ routes / user')(redis);


解决方案

我们几乎总是看到 require() module.exports 一起使用,但您不必。当您不导出任何内容时,导入模块中的代码仍将运行,但是您不能将导入绑定到变量并与之进行交互。



以下 Foo.js 模块:

  var foo = {}; 

foo.greet = function(){
console.log('Hello from Foo!');
}

foo.greet();

我可以在我的主文件中导入此模块,如下所示:

  require('./ foo'); 

如果我运行这个主文件, Foo.js 模块将运行,您好,从Foo!将打印到控制台。



但是,我不能直接与foo对象交互。以下代码将无法正常工作:

  require('./ foo'); 
foo.greet(); // ReferenceError,foo未定义

我可以将模块导入绑定到一个变量,但即使这样将无法正常工作:

  var foo = require('./ foo'); 
foo.greet(); // TypeError,foo.greet不是一个函数

要使其正常工作,我需要导出使用您熟悉的 module.exports 的模块中的foo对象。



这表明你不需要从模块导出任何东西,就像在需要时,不需要将导入的模块绑定到变量。不同的是,如果您不导出不想在该模块中显示的内容,那么您将无法与导入的模块中的代码进行交互。



在您的问题的代码中,导入Redis的工作原理是因为该模块是独立的,您不需要在代码中与其进行交互。您只需要导入代码即可运行(需要主Redis模块并创建客户端)


I have this code (which works perfectly well) which I've borrowed from an online resource:

var express = require('express');
var bodyParser = require('body-parser');
var logger = require('morgan');

var app = express();

require('./init/db');
require('./init/cache'); //Bring in Redis

//Define Routes
var userRoutes = require('./routes/user');

module.exports = app;

The bit I don't understand is "require" when used in this way? Here is the file it brings in:

//db.js
var mongoose = require('mongoose');
var dbURI = <theURI>;

mongoose.connect(dbURI);

// CONNECTION EVENTS
mongoose.connection.on('connected', function() {
  console.log('Mongoose connected successfully');
});

It's the same with my Redis connection:

//cache.js
var redis  = require("redis");

var redisClient  = redis.createClient(process.env.CACHE_PORT, process.env.CACHE_URL);
redisClient.auth(process.env.CACHE_PASS);

redisClient.on("ready", function () {
  console.log("Cache is connected");
});

but as you can see there is no module.exports anywhere in the db.js or cache.js files! When I google this to understand how it works the examples always talk about module.exports and require together.

Questions

  1. Could someone explain how require works when used on its own like this?

  2. How can I make the cache/Redis connection available so that it can be used in my userRoutes file using something like: var userRoutes = require('./routes/user')(redis);

解决方案

We almost always see require() being used with module.exports, but you don't have to. When you don't export anything, the code in the imported module will still run, but you can't bind the import to a variable and interact with it.

Consider the following Foo.js module :

var foo = {};

foo.greet = function(){
    console.log('Hello from Foo!');
}

foo.greet();

I can import this module in my main file, like so :

require('./foo');

If I run this main file, the code inside the Foo.js module will run, and Hello from Foo! will be printed to the console.

However, I can't interact with the foo object directly. The following code will not work :

require('./foo');
foo.greet(); //ReferenceError, foo is not defined

I can bind the module import to a variable, but even this will not work :

var foo = require('./foo');
foo.greet(); //TypeError, foo.greet is not a function

To get it to work, I need to export the foo object from my module, using the module.exports that you are familiar with.

This demonstrates that you don't need to export anything from your modules, just like you don't need to bind the imported module to a variable when you are requiring it. The difference is that you won't be able to interact with the code in the imported module, if you don't export what you don't want to make visible in that module.

In the code in your question, importing Redis works because that module is self-contained, you don't need to interact with it in your code. You only need to import the code so that it can run (require the main Redis module and create the client)

这篇关于使用需求而不导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 05:29