Skip to content

Instantly share code, notes, and snippets.

@jhartikainen jhartikainen/api.js Secret
Last active Aug 29, 2015

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
You can’t perform that action at this time.