title: How to load JSON data on Node.js
body: <p>I am working to load JSON data using Node/express.js and plot it on map. As first start, I am inspired by the example presented in the repp leaflet-geojson-stream. <a href="https://github.com/tmcw/leaflet-geojson-stream/tree/master/example" rel="nofollow">https://github.com/tmcw/leaflet-geojson-stream/tree/master/example</a></p>
  
  <p>Client:
  <a href="https://github.com/tmcw/leaflet-geojson-stream/blob/master/example/client.js" rel="nofollow">https://github.com/tmcw/leaflet-geojson-stream/blob/master/example/client.js</a></p>
  
  <pre><code>var L = require('leaflet'),
      leafletStream = require('../');
  
  L.Icon.Default.imagePath = 'http://leafletjs.com/dist/images';
  
  window.onload = function() {
      var div = document.body.appendChild(document.createElement('div'));
      div.style.cssText = 'height:500px;';
      var map = L.map(div).setView([0, 0], 2);
      L.tileLayer('http://a.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
      var gj = L.geoJson().addTo(map);
      leafletStream.ajax('/points.geojson', gj)
          .on('end', function() {
          });
  };
  </code></pre>
  
  <p>Server :
  <a href="https://github.com/tmcw/leaflet-geojson-stream/blob/master/example/server.js" rel="nofollow">https://github.com/tmcw/leaflet-geojson-stream/blob/master/example/server.js</a></p>
  
  <pre><code>var express = require('express'),
      browserify = require('browserify'),
      app = express();
  
  app.get('/', function(req, res) {
      res.send('&lt;html&gt;&lt;head&gt;&lt;link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" /&gt;&lt;/head&gt;&lt;body&gt;&lt;script src="bundle.js"&gt;&lt;/script&gt;&lt;/html&gt;');
  });
  
  app.get('/bundle.js', function(req, res) {
      var b = browserify();
      b.add('./client.js');
      b.bundle().pipe(res);
  });
  
  app.get('/points.geojson', function(req, res) {
      res.write('{"type":"FeatureCollection","features":[');
      var i = 0, die = 0;
      function send() {
          if (++i &gt; 20) {
              res.write(JSON.stringify(randomFeature()) + '\n,\n');
              i = 0;
          } else {
              // it seems like writing newlines here causes the buffer to
              // flush
              res.write('\n');
          }
          if (die++ &gt; 1000) {
              res.write(JSON.stringify(randomFeature()));
              res.write(']');
              res.end();
              return;
          }
          setTimeout(send, 10);
      }
      send();
  });
  
  app.listen(3000);
  
  function randomFeature() {
      return {
          type: 'Feature',
          geometry: {
              type: 'Point',
              coordinates: [
                  (Math.random() - 0.5) * 360,
                  (Math.random() - 0.5) * 180
              ]
          },
          properties: {}
      };
  }
  </code></pre>
  
  <p>In the project, they create random json file. I wanted to read json file, then plot it. The reason I want to "Stream data" is to deal with the size of file (I know that there is better and easier ways to load json data), But I wanted to use this module.</p>
  
  <p>I modified the server script :</p>
  
  <pre><code>var express = require('express'),
      browserify = require('browserify'),
      app = express();
  
  app.get('/', function(req, res) {
      res.send('&lt;html&gt;&lt;head&gt;&lt;link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" /&gt;&lt;/head&gt;&lt;body&gt;&lt;script src="bundle.js"&gt;&lt;/script&gt;&lt;/html&gt;');
  });
  
  app.get('/bundle.js', function(req, res) {
      var b = browserify();
      b.add('./client.js');
      b.bundle().pipe(res);
  });
  
  var o = require('../output_.geojson');
  
  app.get('/points.geojson', function(req, res) {
           res.json(o);
  });
  
  app.listen(3000);
  
      res.write('');
  </code></pre>
  
  <p>But I am getting error :</p>
  
  <pre><code>/Users/macbook/leaflet-geojson-stream/output_.geojson:1
  (function (exports, require, module, __filename, __dirname) { "type":"FeatureC
                                                                      ^
  SyntaxError: Unexpected token :
      at Module._compile (module.js:439:25)
      at Object.Module._extensions..js (module.js:474:10)
      at Module.load (module.js:356:32)
      at Function.Module._load (module.js:312:12)
      at Module.require (module.js:364:17)
      at require (module.js:380:17)
      at Object.&lt;anonymous&gt; (/Users/macbook/leaflet-geojson-stream/example/server.js:15:9)
      at Module._compile (module.js:456:26)
      at Object.Module._extensions..js (module.js:474:10)
      at Module.load (module.js:356:32)
  </code></pre>
  
  <p>Can anyone give a hint on what should I do in order to read and load json external file to plot the data.</p>