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
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: