Skip to content

Instantly share code, notes, and snippets.

@jhartikainen
Last active August 29, 2015 14:14
Embed
What would you like to do?
node http testing sample
var http = require('http');
module.exports = {
get: function(callback) {
var req = http.request({
hostname: 'jsonplaceholder.typicode.com',
path: '/posts/1'
}, function(response) {
var data = '';
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
callback(null, JSON.parse(data));
});
});
req.on('error', function(err) {
callback(err);
});
req.end();
},
post: function(data, callback) {
var req = http.request({
hostname: 'jsonplaceholder.typicode.com',
path: '/posts',
method: 'POST'
}, function(response) {
var data = '';
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
callback(null, JSON.parse(data));
});
});
req.write(JSON.stringify(data));
req.end();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment