read

If you are reading this, you have obviously clicked already once to get to this post. So you only have 1 click left – use it on one of the sample links below.

Then you can experiment more with haiku-http, read about haiku-http vision and design, or get the code on GitHub.

Hello, world

Here is the ‘Hello, world’ rite of passage:

http://haiku.cloudapp.net/?x-haiku-handler=https://raw.github.com/tjanczuk/haiku-http/master/samples/haikus/hello.js

1 res.writeHead(200)  
2 res.end('Hello, world!\n')

War and peace

How many times do the words “war” and “peace” appear on http://reuters.com today?

War:

http://haiku.cloudapp.net/?x-haiku-handler=https://raw.github.com/tjanczuk/haiku-http/master/samples/haikus/request.js&word=war

Peace:

http://haiku.cloudapp.net/?x-haiku-handler=https://raw.github.com/tjanczuk/haiku-http/master/samples/haikus/request.js&word=peace

 1 var query = require('url').parse(req.url, true).query  
 2 var word = query.word || 'the'  
 3 var request = require('request')  
 4 request('http://www.reuters.com', function (error, response, body) {  
 5     if (error || response.statusCode !== 200) {  
 6         res.writeHead(500)  
 7         res.end('Unexpected error getting http://reuters.com.\n')  
 8     }  
 9     else {  
10         var count = 0, index = 0  
11         while (0 !== (index = (body.indexOf(word, index) + 1)))  
12             count++  
13         res.writeHead(200)  
14         res.end('Number of times the word "' + word + '" occurs on http://reuters.com is: ' + count + '\n')  
15     }  
16 })

Fetch data from MongoDB

Return documents from MongoDB that match search criteria.

All documents:

http://haiku.cloudapp.net/?x-haiku-handler=https://raw.github.com/tjanczuk/haiku-http/master/samples/haikus/mongo.js

Only documents for ‘app1.com’ host:

http://haiku.cloudapp.net/?x-haiku-handler=https://raw.github.com/tjanczuk/haiku-http/master/samples/haikus/mongo.js&host=app1.com

 1 var query = require('url').parse(req.url, true).query  
 2 var mongoUrl = query['db'] || 'mongodb://arr:arr@staff.mongohq.com:10024/arr'  
 3 var filter = query['host'] ? { hosts: query['host'] } : {}  
 4   
 5 require('mongodb').connect(mongoUrl, function (err, db) {  
 6     if (notError(err))  
 7         db.collection('apps', function (err, apps) {  
 8             if (notError(err))  
 9                 apps.find(filter).toArray(function (err, docs) {  
10                     if (notError(err)) {  
11                         res.writeHead(200)  
12                         res.end(JSON.stringify(docs))  
13                     }  
14                 })  
15         })  
16 })  
17   
18 function notError(err) {  
19     if (err) {  
20         res.writeHead(500)  
21         res.end(err)  
22     }  
23     return !err  
24 }

Time for more

Experiment more with haiku-http, read about haiku-http vision and design, or get the code on GitHub.

Comments welcome!

Blog Logo

Tomasz Janczuk


Published

Image

Tomek on Software

Software - shaken, not stirred

Back to Overview