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?

1

1 Answer 1

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);
      })
    }
};
Sign up to request clarification or add additional context in comments.

3 Comments

Isn't dependency injection a little unclean in the long run where you might have to pass on more and more dependencies?
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 :)
It requires you to write a little more code, that is true, but explicitness will definitely change how readable and understandable your code is

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.