0

I'm fairly new to nodejs. Writing my first application. I'm pretty used to php.

In order to keep code organized and clean, i always write functions in separate files and include them as required in php.

However, in nodejs i've had to require them like i would require a module. For example.

functions.js

module.exports = {
check_db : function(key){

},

check_cache : function(key){
    memcached.get(key,function(err, data){
        console.log(data);
    });
},

};

Included that in the main app like so

// Establish connection with cache and database
const mysql = require('mysql2');
const Memcached = require('memcached');
const memcached = new Memcached('localhost:11211');
const bb = require('bot-brother');

//Load the database cache functions
const dbc = require("./functions");
dbc.check_cache(123);

Now i can access the functions from dbc from the main app file, but i cannot use modules that have been required in the main app from the functions file. I get an error that memcached is not defined.

How can i go about solving this?

Marshall Mathews
  • 347
  • 5
  • 18
  • Possible duplicate of [Node.js "require" function and parameters](https://stackoverflow.com/questions/7367850/node-js-require-function-and-parameters) – James Gould May 26 '17 at 09:23

1 Answers1

0

Simple solution, you can require("memcached") in the functions.js file and create the server here. But I wouldn't go with this solution, as, if you need memcache somewhere else, you would have opened many connections on the memcache server.

Another, and cleaner solution IMO, is to inject the memcache dependency into your services (or functions as you call them). (this practice is called dependency injection if you want to learn about it and what are the benefits of it)

Here is how it would work:

  • you still create the memcache connection in the main file ;
  • instead of exporting a raw json object in your functions.js, you export a function that takes an argument (here memcache)
  • in your main file, you require that function and call it to get the service you want.

Here is what the code would look like:

main.js

//Load the database cache functions
const dbcFactory = require("./functions");
const dbc = dbcFactory(memcached)

functions.js

module.exports = function (memcached) {
  return {
    check_db : function(key){},

    check_cache : function(key){
      memcached.get(key,function(err, data){
        console.log(data);
      })
    }
};
atomrc
  • 2,543
  • 1
  • 16
  • 20
  • Isn't dependency injection a little unclean in the long run where you might have to pass on more and more dependencies? – Marshall Mathews May 26 '17 at 09:34
  • The key benefits of DI (dependency injection) is that **everything is explicit** and that every service you have is easily testable (you can inject mocked dependencies in the test env). I'd say that if your code gets messy with DI, it reveals that your general architecture as problems and you might need to re-think some parts :) – atomrc May 26 '17 at 09:40
  • It requires you to write a little more code, that is true, but explicitness will definitely change how readable and understandable your code is – atomrc May 26 '17 at 09:42