The Artima Developer Community
Sponsored Link

Java Buzz Forum
quickly hacking a prototype json server with Node.js

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Marc Logemann

Posts: 594
Nickname: loge
Registered: Sep, 2002

Marc Logemann is founder of www.logentis.de a Java consultancy
quickly hacking a prototype json server with Node.js Posted: Jul 28, 2010 2:25 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Marc Logemann.
Original Post: quickly hacking a prototype json server with Node.js
Feed Title: Logemann Blog
Feed URL: http://feeds.feedburner.com/LogemannBlog
Feed Description: Marc Logemann's thoughts on java and other stuff
Latest Java Buzz Posts
Latest Java Buzz Posts by Marc Logemann
Latest Posts From Logemann Blog

Advertisement
You are developing a REST client w/o having the "real" backend ready? But you dont want to start up and configure an apache server and of course you dont want to write a full stack java application just for testing your client? Then why not using node.js. In this little example i registered two REST urls with the server to send back some json data.

var http = require('http');
var sys = require('sys');
var server = http.createServer(function(req, res) {
    
 if(req.url == '/parcel/1') {
  res.writeHead(200, {
         'Content-Type': 'application/json'
     });
     res.end(JSON.stringify(getTrackInfoJson()));
 } else if(req.url = '/stats/last6'){
  res.writeHead(200, {
          'Content-Type': 'application/json'
     });
     res.end(JSON.stringify(getStatsInfoJson())); 
 }
});
server.listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');

function getStatsInfoJson() {
 "statinfo":{
  "type":"bar",
  "category":"parcelslast6",
  "data" : [232, 242, 32, 342, 100, 98]
 }
}

function getTrackInfoJson() {
 return { 
     "trackinfo":{
         "parcelnr":123456,
         "provider":"DHL",
         "trackdetails":{
             "trackdetail":[
                 {
                     "date":"01.01.2010",
                     "info":"Got parcel from Customer"
                 },
                 {
                     "date":"02.01.2010",
                     "info":"Shipped to Target depot"
                 },
                 {
                     "date":"03.01.2010",
                     "info":"Delivered to Customer"
                 }
             ]
         }
     }
 };
}

These two URLs will be answered by our little json-http server:

http://localhost:8124/parcel/1
http://localhost:8124/stats/last6

Of course you should have installed node.js before. If you have done that right, you can start the server by typing: node myfile.js

Read: quickly hacking a prototype json server with Node.js

Topic: Popular White iPhone Delayed Until Late 2010 Previous Topic   Next Topic Topic: Moody's upgrades BMC Software to “Baa2″ from “Baa3″

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use