net模块

建立TCP的链接

1 发送消息的服务

2 接收消息

2 建立http的链接让浏览器进行访问

复制代码
import net from 'net'

const html = `<h1>TCP</h1>`

const respinseHeaders = [

    'HTTP/1.1 200 OK',
    
    'Content-Type:text/html',
    
    'Content-Length':' + html.length,
    
    '\r\n',

    html

]

const http = net.createServer(socket=>{

    socket.on('data',(e)=>{

        if(/GET/.test(e.toString())){

            socket.write(responseHeaders,join('\r\n'))

            socket.end()
        }

        console.log(e.toString())
    })

})

http.listen(80,()=>{

    console.log('Server is listening on port 80')
})
相关推荐
贩卖纯净水.1 小时前
Webpack的基本使用 - babel
前端·webpack·node.js
贩卖纯净水.3 小时前
Webpack依赖
前端·webpack·node.js
笑醉踏歌行5 小时前
NVM,Node.Js 管理工具
运维·ubuntu·node.js
chxii7 小时前
1.4 Node.js 的 TCP 和 UDP
node.js
xd0000220 小时前
11. vue pinia 和react redux、jotai对比
node.js
程序猿小D21 小时前
第16节 Node.js 文件系统
linux·服务器·前端·node.js·编辑器·vim
前端老六喔1 天前
🎉 开源项目推荐 | 让你的 TypeScript/React 项目瘦身更简单!
node.js·前端工程化
醉书生ꦿ℘゜এ1 天前
npm error Cannot read properties of null (reading ‘matches‘)
前端·npm·node.js
超级土豆粉1 天前
从0到1写一个适用于Node.js的User Agent生成库
linux·ubuntu·node.js
空中湖1 天前
‘pnpm‘ 不是内部或外部命令,也不是可运行的程序
npm·node.js