Skip to content

Instantly share code, notes, and snippets.

@aacerox
Created August 9, 2013 11:09
Show Gist options
  • Save aacerox/f2fdf78e651804e5b004 to your computer and use it in GitHub Desktop.
Save aacerox/f2fdf78e651804e5b004 to your computer and use it in GitHub Desktop.
Test node-rest-client POST methods
var restClient = require("node-rest-client").Client;
var appgAPIClient = new restClient();
var args = {
data: { test: "hello" }
};
appgAPIClient.post("http://localhost:4444/json?post", args, function(data,response) {
console.log("data=>", data);
//console.log("response", response);
});
var http = require('http'),
fs = require('fs');
// Create an HTTP server
var httpSrv = http.createServer(function (req, res) {
console.log("req.url", req.url);
RouteManager.findRoute(req,res);
});
var RouteManager ={
"findRoute":function(req,res){
var handler = this.routes[req.url];
if (!handler) throw "cannot find route " + req.url;
handler.call(this,req,res);
},
"routes":{
"/json":function(req,res){
var message = fs.readFileSync('./message.json','utf8');
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(message.toString());
res.end();
},
"/xml":function(req,res){
var message = fs.readFileSync('./message.xml','utf8');
res.writeHead(200, {'Content-Type': 'application/xml'});
res.write(message.toString());
res.end();
},
"/120/json?arg1=hello&arg2=world":function(req,res){
if (!req.headers["test-header"]) throw "no test-header found!!";
res.setHeader("test-response-header",req.headers["test-header"]);
this.routes["/json"](req,res);
},
//THIS IS OUR METHOD
"/json?post":function(req,res){
req.on('data',function(data){
console.log("[SERVER] data = ", data);
res.writeHead(200, {'Content-Type': 'application/json'});
//res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(data.toString());
res.end();
});
}
}
};
httpSrv.on('error',function(err){
console.error('error starting http test server',err);
});
httpSrv.listen(4444);
console.log('http server Listening on port ' + 4444);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment