Node.js HTTP 模块
示例
创建一个在您的计算机 8080 端口上监听的服务器。
当访问 8080 端口时,请以“Hello World!”作为响应写回
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World!');
res.end();
}).listen(8080);
运行示例 »
定义和用法
HTTP 模块提供了一种通过 HTTP(超文本传输协议)传输数据的方式。
语法
在您的应用程序中包含 HTTP 模块的语法
var http = require('http');
HTTP 属性和方法
方法 | 描述 |
---|---|
createClient() | 已弃用。 创建 HTTP 客户端 |
createServer() | 创建 HTTP 服务器 |
get() | 设置方法为 GET,并返回包含用户请求的对象 |
globalAgent | 返回 HTTP Agent |
request() | 返回包含用户请求的对象 |