Permalink
Browse files

Node.js example

  • Loading branch information...
1 parent 5b92171 commit 2dc5fe10c5d7d0292212b46a2b95337abd13b628 @aeden committed May 7, 2012
Showing with 32 additions and 0 deletions.
  1. +1 −0 README.md
  2. +19 −0 echo/node/echo/README.md
  3. +12 −0 echo/node/echo/app.js
View
1 README.md
@@ -11,3 +11,4 @@ Example is available in the following languages:
* clojure
* ruby
* python
+* javascript (node.js)
View
19 echo/node/echo/README.md
@@ -0,0 +1,19 @@
+# echo
+
+Web service written in JavaScript (to run on Node.js) that parses and echoes JSON data.
+
+## Usage
+
+Install node and then run the service:
+
+ node app.js
+
+Call the service from curl:
+
+ curl -i "http://127.0.0.1:3000" -d '{"foo":"bar"}'
+
+## License
+
+Copyright (C) 2012 Anthony Eden
+
+Distributed under the MIT License.
View
12 echo/node/echo/app.js
@@ -0,0 +1,12 @@
+var http = require('http');
+
+http.createServer(function (request, response) {
+ var data = '';
+ request.addListener('data', function(chunk) { data += chunk; });
+ request.addListener('end', function() {
+ response.writeHead(200, {'Content-Type': 'application/json'});
+ response.end(JSON.stringify(JSON.parse(data)));
+ });
+}).listen(3000);
+
+console.log('Server running at http://127.0.0.1:3000/');

0 comments on commit 2dc5fe1

Please sign in to comment.