diff options
author | Mark Engel <mark.c.engel@gmail.com> | 2011-01-21 15:26:25 +0100 |
---|---|---|
committer | Mark Engel <mark.c.engel@gmail.com> | 2011-01-21 15:26:25 +0100 |
commit | 6ed48d9e3c9ee669d25323fb547acaeff2194437 (patch) | |
tree | e3b7c8611f43ecee48d8c6d66cb16e93e4edad78 /src/nodejs | |
parent | 9f2167aef63470b7d4cb3498aac736d284026ca9 (diff) | |
download | osm-xapi-6ed48d9e3c9ee669d25323fb547acaeff2194437.tar.gz osm-xapi-6ed48d9e3c9ee669d25323fb547acaeff2194437.tar.xz osm-xapi-6ed48d9e3c9ee669d25323fb547acaeff2194437.zip |
created function db_connect which wraps connection to the database
Diffstat (limited to '')
-rw-r--r-- | src/nodejs/no1.js | 55 |
1 files changed, 23 insertions, 32 deletions
diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js index 7be26a4..48f8b7f 100644 --- a/src/nodejs/no1.js +++ b/src/nodejs/no1.js @@ -46,23 +46,15 @@ function createNodesForWayQuery(nodes) { return "SELECT id, tstamp, version, changeset_id, hstore_to_array(tags) as tags, X(geom) as lat, Y(geom) as lon FROM nodes WHERE id = ANY('" + nodes + "');"; } - - function nodeWorldHandler(req, res, key, value) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end(' key:' +key +' value:'+value+'\n'); } function nodeBboxHandler(req, res, key, value, left, bottom, right, top) { - pg.connect(connectionString, function(err,client) { - - if (err) { - console.log(err); - res.writeHead(404,{}); - res.end('\n'); - } - else { - //console.log(createNodeBboxQuery(key, value, left, bottom, right, top)); + console.log("nodeBboxHandler"); + db_connect(res, function() { + //console.log(createNodeBboxQuery(key, value, left, bottom, right, top)); var success = false; var query = client.query(createNodeBboxQuery(key, value, left, bottom, right, top)); @@ -111,33 +103,36 @@ function nodeBboxHandler(req, res, key, value, left, bottom, right, top) { .att('k',escape(temp[x])) .att('v',escape(temp[x+1])); } - //for(var x=0; x< tags.length;x++) - //console.log(tags[x]); - /*node.ele('tag') - .att('k',tags[x][0]) - .att('v',tags[x][1]); - */ + res.write(builder.toString({ pretty: true })); - //res.write(builder.toString()); - }); - } + }); }); } function wayWorldHandler(req, res, key, value) { - res.writeHead(200, {'Content-Type': 'text/plain'}); - } -function wayBboxHandler(req, res, key, value, left, bottom, right, top) { - pg.connect(connectionString, function(err,client) { + +function connectionError(err, res) { + console.log(err); + console.log("foobar"); +} + +function db_connect(res, callback) { + pg.connect(connectionString, function(err, client) { if(err) { - console.log(err); + console.log(err['message']); res.writeHead(404,{}); res.end(); + } else { + callback(); } - else { - var count = 0; + }); +} + +function wayBboxHandler(req, res, key, value, left, bottom, right, top) { + db_connect(res, function() { + var count = 0; var success = false; //console.log(createWayBboxQuery(key, value, left, bottom, right, top)); var query = client.query(createWayBboxQuery(key, value, left, bottom, right, top)); @@ -223,11 +218,7 @@ function wayBboxHandler(req, res, key, value, left, bottom, right, top) { res.write(builder.toString({pretty:'true'})); }); - - } - - }); - + }); } function relationWorldHandler(req, res, key, value) { |