Skip to content

Instantly share code, notes, and snippets.

@watagashi
Last active December 13, 2016 17:06
Show Gist options
  • Save watagashi/22090e37bd87af320ecd7f7825a3f83d to your computer and use it in GitHub Desktop.
Save watagashi/22090e37bd87af320ecd7f7825a3f83d to your computer and use it in GitHub Desktop.
% node server.js & node client.js
Server running
{
"host": "localhost:4321",
"accept": "application/json",
"content-type": "application/json",
"content-length": "37",
"connection": "close"
}
body: [{"a":1,"b":true,"c":"x","d":"漢字"}]
33 characters
37 bytes
var request = require('request').defaults({
baseUrl: 'http://localhost:4321'
});
request('/', {
method: 'POST',
body: {
a: 1,
b: true,
c: 'x',
d: '漢字'
},
json: true
}, function(err, res, body) {
if (err) console.error(err);
});
var server = require('http').createServer(function(req, res) {
var rawData = '';
req.on('data', function(chunk) {
rawData += chunk;
}).on('end', function() {
console.log('body: [' + rawData + ']');
console.log(rawData.length + ' characters');
console.log(Buffer.from(rawData, 'utf8').length + ' bytes');
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
console.log(JSON.stringify(req.headers, null, ' '));
});
server.listen(4321, 'localhost', function() {
console.log('Server running');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment