2014-03-31 3 views
0

Node.js/express.js에서 geojson 파일을 읽으려고합니다. 나는 싶어express.js에서 geojson 파일을로드하는 방법

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

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

을 express.js를 사용하여로드 (또는 적어도 JSON이 함수 내에서 렌더링) JSON.parse.But를 내가 싶어 사용하지 않는 "output.geojson"읽고있다 그러나 나는이 무엇입니까 오류 :

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) 

geojson 파일은 다음과 같습니다.

{ 
"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" 
} 
.... 
.... 
} 

geojson 파일을로드하려면 어떻게해야합니까?

답변

1

현재 '요구'하여 전체 JSON 파일을 메모리에로드하고 있습니다.

var fs = require('fs'); 

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

는 또한 /output.geojson의 내용이 실제로 유효 JSON 있는지 확인 : 그것은 큰 그래서 fs.createReadStream 기능을 사용하기 때문에

대신 당신은 파일을 스트리밍 할 수 있습니다. JSONLint을 사용하여 파일을 '{'또는 '['로 지정하고 Javascript 기능을 내부에 포함하지 않아야합니다.

관련 문제