I am writing a Node.js HTTP server. For HEAD requests I would use the GET code path especially for calculating the content size etc. How to skip sending the body afterwards? I imagine the following example
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World\n');
if(req.method === 'HEAD') {
res.dropBody(); // omit the body
}
res.end(); // just send the headers
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
How about turning it around?
if (req.method === 'GET') {
res.write('Hello World\n');
}