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')
})
相关推荐
艾小码9 小时前
手把手教你实现一个EventEmitter,彻底告别复杂事件管理!
前端·javascript·node.js
前端小哲10 小时前
MCP从入门到实战
node.js·ai编程
dasseinzumtode11 小时前
nestJS 使用ExcelJS 实现数据的excel导出功能
前端·后端·node.js
梅孔立16 小时前
服务器不支持node.js16以上版本安装?用Docker轻松部署Node.js 20+环境运行Strapi项目
服务器·docker·node.js
XiaoMu_00116 小时前
基于Node.js和Three.js的3D模型网页预览器
javascript·3d·node.js
卿·静16 小时前
Node.js对接即梦AI实现“千军万马”视频
前端·javascript·人工智能·后端·node.js
lvlv_feifei16 小时前
N8N macOS (Apple Silicon) 完整安装配置教程
node.js·workflow
Cosmoshhhyyy1 天前
Node.js 18+安装及Claude国内镜像使用、idea中claude插件下载指南
node.js
徐_三岁1 天前
关于npm的钩子函数
前端·npm·node.js
不买Huracan不改名2 天前
安装Codex(需要用npm)
前端·npm·node.js