The documentation for the method writes, "If data is specified, it is equivalent to calling response.write(data, encoding) followed by response.end()."
And the doc describes the response.write(chunk, [encoding]) as,
chunk can be a string or a buffer. If chunk is a string, the second parameter specifies how to encode it into a byte stream. By default the encoding is 'utf8'.
I still do not get how to use this method given the description. Can someone give a very simple example of a set of working parameters in this case?
hm, simple:
res.write('<h1>It works!</h1>', 'utf8');
res.end();
is equivalent to
res.end('<h1>It works!</h1>', 'utf8');
response.end(data, encoding) will do the following:
response.write(data, encoding);
response.end();
Sample code:
var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, { "Content-Type": "text/plain" });
// 1st way
response.write('Hello World\n');
response.end();
// 2nd way, equivalent
//response.end('Hello World\n');
});
server.listen(8000);
console.log('running');