Answer the question
In order to leave comments, you need to log in
How to use single class in express js?
I have app.ts and several classes for routing
, they must use a connection to the database (mongoose), but how can I implement so that the class (service) for working with the database is passed to these classes for routing?
Answer the question
In order to leave comments, you need to log in
Any exported class will be a singleton.
class Service {}
export.module = new Service();
class Service {}
let service = () => {
let instance
function init() {
let opt = new Service()
return opt
}
return {
getInstance: function () {
if (!instance) {
instance = init()
}
return instance
}
}
}
module.export = service().getInstance()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question