Created
February 15, 2015 13:32
-
-
Save kshirish/9dae41ed1f6346bc9624 to your computer and use it in GitHub Desktop.
node api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
// list all status codes | |
http.STATUS_CODES | |
var server = http.createServer(function(req, res){ | |
// get header info | |
console.log(req.headers); | |
//get method | |
console.log(req.method); | |
//get url | |
console.log(req.url); | |
console.log('listen'); | |
res.writeHead(200, {'Content-Type':'text/plain'}); | |
res.end('okay'); | |
}); | |
server.listen(9990, function(){ | |
console.log('listen'); | |
}); | |
server.on('connect', function(){ | |
console.log('on connect'); | |
}); | |
var postData = querystring.stringify({name: 'harish', city: 'NY'}); | |
var options = { | |
hostname: 'www.google.com', | |
port: '80', | |
path: '/upload', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': postData.length | |
} | |
}; | |
var req = http.request(options, function(res){ | |
res.setEncoding('utf-8'); | |
res.on('data', function(chunk){ | |
console.log(chunk); | |
}); | |
}); | |
req.on('error', function(e){ | |
console.log('I got an error ', e); | |
}); | |
req.write(postData); | |
req.end(); | |
// specifically GET request | |
http.get("http://www.google.com/", function(res) { | |
console.log("Got response: " + http.STATUS_CODES[ res.statusCode ]); | |
}).on('error', function(e) { | |
console.log("Got error: " + e.message); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment