创建客户端
原创2026/3/5小于 1 分钟

客户端
//client.js
var http = require('http')
// 用于请求的选项
var options = {
host: 'localhost',
port: '3030',
path: '/',
}
// 处理响应的回调函数
var callback = function (response) {
// 不断更新数据
var body = ''
response.on('data', function (data) {
body += data
})
response.on('end', function () {
// 数据接收完成
console.log(body)
})
}
// 向服务端发送请求
var req = http.request(options, callback)
req.end()服务端
//server.js
const http = require('http')
const url = require('url')
const server = http.createServer(function (req, res) {
const { pathname } = url.parse(req.url)
if (pathname == '/') {
res.write('hello')
res.write('world')
res.end()
}
})
server.listen('3030', function () {
console.log('服务器正在监听3030端口')
})至此,本章节的学习就到此结束了,如有疑惑,可对接技术客服进行相关咨询。