Friday 14 July 2017

Is using module.exports for a singleton a bad idea?

Trying to get a better handle on nodejs, i am wondering is this a bad practice?Bascially i have something similar to this:// services/foo-service.js class FooService { constructor() { } doSomething() { // does something } } module.exports = FooService; // services/index.js const FooService = require('./foo-service'); module.exports = { foo: new FooService() }; // in some other file const services = require('../services'); const bar = function bar() { services.foo.doSomething(); } Is this an okay way to export and utilize the service as a singleton?

Submitted July 15, 2017 at 06:05AM by frankimthetank

No comments:

Post a Comment