zl程序教程

您现在的位置是:首页 >  后端

当前栏目

node.js中的http.response.write方法使用说明

JS方法NodeHTTP 使用 说明 Write response
2023-06-13 09:15:32 时间

方法说明:

向请求的客户端发送响应内容。

在response.end()之前,response.write()可以被执行多次。

语法:

复制代码代码如下:


response.write(chunk,[encoding])

参数:

chunk                     是一个buffer或字符串,表示发送的内容

encoding               如果chunk是字符串,就需要指定encoding来说明它的编码方式,默认utf-8

例子:

复制代码代码如下:


varhttp=require("http");
http.createServer(function(req,res){
 res.writeHead(200,{"Content-type":"text/html"});
 res.write("<h1>Node.js</h1>");
 res.end("<p>HelloWorld</p>");
}).listen(3000);