0

I created a module in nodejs where I wish to expose it's constants too. But this particular module contains a dependency which is provided at construction time i.e. dependency injection.

this is module1

const STORE_TYPE = {
  STORE1: 1,
  STORE2: 2
};

function service(dependency1) {
  this.dep = dependency1;
}

service.prototype.doSomething = function(param1, store) {
  if (STORE_TYPE.STORE1 == store) {
    return this.dep.get(param1);
  } else {
    return "something";
  }
};

module.exports = service;

I'm using module1 here:

var dep = require('./dep');
var dep1 = new otherService(dep);
var service = require('./service')(dep1);

function getData() {
  return service.doSomething(id, /*this is module1 constant*/1);
}

How do I refere to module1's constants if module1 has a constructor.

I don't wish to add separate method only to create service since callee needs to perform multiple steps.

1 Answer 1

2

Try this:

service.js

exports.STORE_TYPE = {
  STORE1: 1,
  STORE2: 2
};

exports.service = function service(dependency1) {
  this.dep = dependency1;
}

service.prototype.doSomething = function(param1, store) {
  if (STORE_TYPE.STORE1 == store) {
    return this.dep.get(param1);
  } else {
    return "something";
  }
};

Using that module

app.js

const service = require('./service').service;

const STORE_STYLE = require('./service').STORE_TYPE;
Sign up to request clarification or add additional context in comments.

Comments

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.