Skip to content

Instantly share code, notes, and snippets.

@jhartikainen
Last active August 29, 2015 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhartikainen/1d3694f4083da3c82fe2 to your computer and use it in GitHub Desktop.
Save jhartikainen/1d3694f4083da3c82fe2 to your computer and use it in GitHub Desktop.
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