服务端发送
// node http写服务端
const http = require('http');
http.createServer(function(req,res){
res.writeHead(200, {
'Content-Type': 'text/plain;charset=utf-8'
});
// 发送数据
res.end("hello Http");
}).listen(8080);
console.log('Server is listening on' + 8080);
客户端接收
// node http写客户端
const http = require('http');
const options = {
host: 'localhost',
port: '8080',
path: '/',
method: 'GET'
}
// 向服务端发起请求 接收数据
const req = http.request(options,(res)=>{
res.setEncoding();
res.on('data',(data)=>{
console.log("接受数据" + data);
})
})
req.end();```