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
Could someone explain how require works when used on its own like this?
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);
You can directly run the code with require statement. To run a program containing import statement you have to use experimental module feature flag. If you want to use require module then you have to save file with '. js' extension.
One of the major differences between require() and import() is that require() can be called from anywhere inside the program whereas import() cannot be called conditionally, it always runs at the beginning of the file. To use the require() statement, a module must be saved with . js extension as opposed to .
The require module, which appears to be available on the global scope — no need to require('require') . The module module, which also appears to be available on the global scope — no need to require('module') .
When we want to export a single class/variable/function from one module to another module, we use the module. exports way. When we want to export multiple variables/functions from one module to another, we use exports way.
Could someone explain what is happening with this code? In other words, how does require work when not used with export.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With