0

Pardon me if this is a completely idiotic question but I just can't figure it out.

Background Info

I'm writing an app in Node.js with the Express framework. I have a URI mapping designed to analyze some Twitter data for sentiment on a given subject in some aggregation units and return the GeoJSON object containing these results to the client.

Here are some details on the data.

  • Aggregation units, States in the US, are stored as Points in a GeoJSON file, loaded via a require call. These points will later be used to make markers in a Leaflet map.
  • Tweets are stored in a MongoDB database, accessed using the mongodb driver.

And this is the process for altering these GeoJSON Points

  • Load the GeoJSON with require
  • Query the MongoDB database for appropriate tweets
  • Iterate over the features in the GeoJSON file
    • Iterate over the tweets in the result set
      • If the tweet is from within that feature
        • Analyze the sentiment within
        • Classify the feature using the sentiment
        • Set marker.properties.class to the classification
  • Write the altered GeoJSON to the response

Problem

So here's my problem, the GeoJSON I load with require is never changed.

Question

Is data loaded via a call to require mutable? If not, is there a simple way to make it mutable, and by that I mean simpler than copying the object?

Bonus question

Does anyone know why console.log() doesn't work when a Node server is running with the Express framework? I swear it did before.

Community
  • 1
  • 1
RyanMullins
  • 315
  • 1
  • 3
  • 15

1 Answers1

2

Modules can be deleted from cache. You just need to do:

delete require.cache[moduleName];

Anyway this is not the best approach.

It is much better to export a constructor function and instantiate a new object when needed. For example:

//in geo.js
function Geo() {...}
module.exports = Geo;

//in another module
var Geo = require('./geo.js');
var geo = new Geo();
lortabac
  • 589
  • 4
  • 16