0

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?

4

2 回答 2

1

hm, simple:

res.write('<h1>It works!</h1>', 'utf8');

res.end();

is equivalent to

res.end('<h1>It works!</h1>', 'utf8');

于 2013-08-05T18:26:13.110 回答
1

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');
于 2013-08-05T18:29:37.577 回答