1

I am try to load country names and want to save them in static variable. So that I do not hit in my database again and again. I am using express Js. Please suggest me How can i load country name in optimize ways

2
  • 1
    share your code Commented Jan 6, 2020 at 11:40
  • Sorry I cannot share Company code but it is very similar to replied answer Commented Jan 7, 2020 at 12:09

1 Answer 1

2

In node.js, modules are cached after the first time they are loaded. Every call to import/require will retrieve exactly the same object. A good way to achieve this is:

app.js

var app = require('express')(),
    server = require('http').createServer(app);
var lookup=require('./lookup.js');

server.listen(80, function() {
    //Just one init call
    lookup.callToDb(function(){
       console.log('ready to go!');
    });

});

lookup.js

callToDb(function (country){
  module.exports=country;
});

and wherever you require: model.js

var countryLookup= require('./lookup.js');
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.