aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorslomo <steve.harrison@gmx.net>2011-01-12 22:58:31 +0100
committerslomo <steve.harrison@gmx.net>2011-01-12 22:58:31 +0100
commit97baff32fd82630497d761f36b5e34466f8ecbc1 (patch)
tree20a11acb9583a7e2846208aa5115eef3b873091c
parent2cb5dd752037ff81329578d1514dac7de0e3ee16 (diff)
downloadosm-xapi-97baff32fd82630497d761f36b5e34466f8ecbc1.tar.gz
osm-xapi-97baff32fd82630497d761f36b5e34466f8ecbc1.tar.xz
osm-xapi-97baff32fd82630497d761f36b5e34466f8ecbc1.zip
some new tries
-rw-r--r--src/nodejs/no1.js17
-rw-r--r--src/nodejs/no2.js106
2 files changed, 123 insertions, 0 deletions
diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js
new file mode 100644
index 0000000..feb3dfb
--- /dev/null
+++ b/src/nodejs/no1.js
@@ -0,0 +1,17 @@
+var clutch = require('clutch');
+
+function helloSomeone(req, res, name,bbox, key, value) {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end('obj:'+name+ ' bbox: '+ bbox + ' key:' +key +' value:'+value+'!\n');
+}
+
+function helloWorld(req, res) {
+ helloSomeone(req, res, 'World');
+}
+
+myRoutes = clutch.route404([['GET /hello/(\\w+)(\\[bbox=(\\d,\\d,\\d,\\d)\\])*\\[(\\w+)=(\\w+)\\]$', helloSomeone],
+ ['GET /hello/$', helloWorld]]);
+
+
+var http = require('http');
+http.createServer(myRoutes).listen(3000, '127.0.0.1');
diff --git a/src/nodejs/no2.js b/src/nodejs/no2.js
new file mode 100644
index 0000000..233b5e1
--- /dev/null
+++ b/src/nodejs/no2.js
@@ -0,0 +1,106 @@
+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');