简体   繁体   中英

How can I load geojson file in expresss.js

I am working to read geojson file in Node.js/express.js. I am reading "output.geojson" I don't wanna use JSON.parse.But I wanna load it using express.js (or at least json render inside this function)

var o = require('output.geojson');

app.get('/points.geojson', function(req, res) {
         res.json(o);
         console.log(res)
});

But I am getting this error :

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.<anonymous> (/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)

The geojson file look like .

{
 "type": "FeatureCollection",
 "features": [{
     "geometry": {
         "type": "MultiPolygon",
         "coordinates": [
             [
                 [
                     [-73.8283219965448, 40.8446061654002],
                     [-73.828397789942, 40.844583182304],
                     [-73.8285477331865, 40.8448132168025],
                     [-73.8284744943625, 40.8448401137412],
                     [-73.8283219965448, 40.8446061654002]
                 ]
             ]
         ]
     },
     "type": "Feature",
 , {
     "geometry": {
         "type": "MultiPolygon",
         "coordinates": [
             [
                 [
                     [-73.832361912256, 40.8488019205992],
                     [-73.832369554769, 40.8487286684528],
                     [-73.8327312374341, 40.8487518102579],
                     [-73.8327304815978, 40.8487590590352],
                     [-73.8327235953166, 40.8488250624279],
                     [-73.832361912256, 40.8488019205992]
                 ]
             ]
         ]
     },
     "type": "Feature"
 }
....
....
}

How can I load geojson file ?

You are currently loading the whole JSON file into memory by 'requiring' it.

Instead you want to stream the file because it is big and so use the fs.createReadStream function:

var fs = require('fs');

app.get('/points.geojson', function(req, res) {
  res.setHeader('Content-Type', 'application/json');
  fs.createReadStream(__dirname + '/output.geojson').pipe(res);
});

Also make sure that the contents of /output.geojson is actually valid JSON. You can use JSONLint to check - the file should with '{' or '[' and NOT have Javascript functions inside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM