1.10 创建客户端 🎉
...小于 1 分钟
1.10 创建客户端 🎉
//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端口')
})
Powered by Waline v2.15.7