1

I searched a lot about the subject and why/how to avoid restfull api caching, butI didn't get a helpful answer.

I built expressjs api to generate random json data using faker module everything went fine for the first request but any refresh on the browser or any additional requests display the same data.

I want with every request generate a random data but I think after the first request the nodejs module is cached.

I'm using

nodejs: the latest version, expressjs: v4.0, faker: the latest

my code as below:

in the router file: router.js

var router = express.Router();
router.get('/name', controller.name);
.
.

in the controller file: json.controller.js

//Get name
var name = require('name.model.js');
exports.name = function(req, res){
  var randomName = name;
  return res.json(200, randomName);
};

in the model file: name.model.js

var faker = require('faker');
var nameModel = {};

nameModel.name = faker.name.findName();

module.exports = nameModel;

Can any one help how to avoid rest api caching? and how to fix it in my case?

Thanx for any help,

1 Answer 1

1

The problem was that I didn't understand moduke.exports and exports in node.js

After reading Understanding module.exports and exports in Node.js

I changed the module to be:

var exports = module.exports = {};

exports.getName = function(){
return faker.name.findName();
}

Voila! that fixed the issue I was facing, and I started getting new data everytime I post a request to the server.

I hope this will help any one needs to understand module.exports and exports

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.