简单的 http 模块

简单的 http 模块

一共分4步 让我们先看第一步

第一步非常简单 我们只要直接引用 http模块就OK啦

// 1.加载  http  模块
const http = require("http");

第二步 使用 http 创建 web 服务器

// 1.加载  http  模块
const http = require("http");
// 2.使用 http 创建 web 服务器
let server = http.createServer();

第三步 监听请求 request 事件

// 1.加载  http  模块
const http = require("http");
// 2.使用 http 创建 web 服务器
let server = http.createServer();
// 3.监听请求 request 事件
server.on("request",function (req,res) {
	// 3.2 设置响应头信息
	res.serHeader("Content-Type","text/pain;charset=utf-8");
	// 3.1 客户端返回数据
	res.end("北京彭于晏 分彭")
})

第四步 监听端口号

//  1.加载 http 模块
const http = require("http");
//  2.使用 http 创建 web服务器
let server = http.createServer();
//  3.监听请求request 事件
server.on("request", function (req, res) {
    // 3.2 设置响应头信息
    res.setHeader("Content-Type", "text/pain;charset=utf-8");
    // 3.1 客户端返回数据
    res.end("传智专修学院 彭于晏");
});
//  4.监听端口号
server.listen(5555, function () {
    console.log("端口启动成功");
});

localnost:5555

然后在浏览器输入localnost:端口号
就可以看到我们的 彭于晏 啦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值