diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/nodejs/no1.js | 69 | ||||
-rw-r--r-- | src/nodejs/no2.js | 106 |
2 files changed, 37 insertions, 138 deletions
diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js index 7be26a4..3b3f8a0 100644 --- a/src/nodejs/no1.js +++ b/src/nodejs/no1.js @@ -5,7 +5,21 @@ var builder = require('xmlbuilder'); var config = require('./config.json'); +// load config +process.argv.forEach( + function (val,index, array){ + if(val=="-c"){ + path = array[index+1]; + console.log(path[0]); + if( path[0] != '/'){ + path = __dirname + '/' + path; + } + config = require(path); + } + }); + var connectionString = config['connectionString']; + console.log("server starting..."); console.log("Connection String: " + connectionString); @@ -46,23 +60,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 +117,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 +232,7 @@ function wayBboxHandler(req, res, key, value, left, bottom, right, top) { res.write(builder.toString({pretty:'true'})); }); - - } - - }); - + }); } function relationWorldHandler(req, res, key, value) { diff --git a/src/nodejs/no2.js b/src/nodejs/no2.js deleted file mode 100644 index 233b5e1..0000000 --- a/src/nodejs/no2.js +++ /dev/null @@ -1,106 +0,0 @@ -var users = [ - { name: 'tj' }, - { name: 'tim' } -]; - -function user(app) { - app.resource('/.:format?', { - 'get' : function(req, res, next) { - switch (req.params.format) { - case 'json': - var body = JSON.stringify(users); - res.writeHead(200, { - 'Content-Type': 'application/json', - 'Content-Length': body.length - }); - res.end(body); - break; - default: - var body = '<ul>' - + users.map(function(user) { - return '<li>' + user.name + '</li>'; - }).join('\n') - + '</ul>'; - res.writeHead(200, { - 'Content-Type': 'text/html', - 'Content-Length': body.length - }); - res.end(body); - } - } - }); - - app.resource('/:id.:format', { - 'get' : function(req, res, next) { - var user = users[req.params.id]; - if (user && req.params.format === 'json') { - user = JSON.stringify(user); - res.writeHead(200, { - 'Content-Type': 'application/json', - 'Content-Length': user.length - }); - res.end(user); - } - else { - // When true is passed, provide control - // back to middleware, skipping route - // match attemps - next(true); - } - } - }) - - app.resource('/\\[:id/:op?', { - 'get' : function(req, res) { - var body = users[req.params.id] - ? users[req.params.id].name - : 'User ' + req.params.id + ' does not exist'; - body = (req.params.op || 'view') + 'ing ' + body; - res.writeHead(200, { - 'Content-Type': 'text/html', - 'Content-Length': body.length - }); - res.end(body, 'utf8'); - } - }) -} - - -function main(app) { - app.resource('/', { - 'get' : function(req, res) { - var examples = [ - '/users', - '/users.json', - '/users/0 (or /users/0/view)', - '/users/0/edit', - '/users/0.json' - ]; - var body = 'Visit one of the following: <ul>' - + examples.map(function(str) { - return '<li>' + str + '</li>' - }).join('\n') - + '</ul>'; - res.writeHead(200, { - 'Content-Type': 'text/html', - 'Content-Length': body.length - }); - res.end(body, 'utf8'); - } - }); -} - - -var connect = require('connect'); -var resource = require('resource-router'); - -var server = connect.createServer( - connect.logger({ buffer: true }), - connect.cache(), - connect.gzip() - ); - -server.use('/users', resource(user)); -server.use(resource(main)); -server.listen(3000); -console.log('Connect server listening on port 3000'); |