1

Basically, I wrote a server that response a js file(object format) to users who made the request. The js file is generated by two config file. I call them config1.js and config2.js.

Here is my code:

var express = require('express');
var app = express();
var _ = require('underscore');

app.use('/config.js', function (req, res) {
    var config1 = require('config1');
    var config2 = require('config2');
    var config = _.extend(config1, config2);
    res.set({'Content-Type': 'application/javascript'});
    res.send(JSON.stringify(config));
});

For what I am understanding, every time I make a request to /config.js, it will fetch the latest code in config1 and config2 file even after I start server. However, if I start server, make some modification in config1.js. then make the request, it will still return me the old code. Can anyone explain and how to fix that? thanks

eded
  • 3,778
  • 8
  • 28
  • 42

2 Answers2

2

You should not use require in order to load your files because it is not its purpose, it caches the loaded file (see this post for more information), that is why you get the same content every time you make a request. Use a tool like concat-files instead, or concat it "by hand" if you prefer.

Community
  • 1
  • 1
Franco
  • 11,845
  • 7
  • 27
  • 33
1

Concat files and extend objects aren't equal operations. You can read the files via 'fs' module, parse objects, extend, and send.

freele
  • 142
  • 7