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')
})
相关推荐
濮水大叔5 小时前
在 DTO 中使用 Form Layout 配置复杂表单,支持 Tabs/Groups/Sections
vue.js·typescript·node.js
Starry-sky(jing)6 小时前
如何判断 Windows 是否安装 Node.js:三种方法 + 常见漏判场景
运维·windows·node.js·环境检测
zzx2006__7 小时前
Ajax➕node.js➕webpack
ajax·webpack·node.js
码农学院7 小时前
基于小程序云开发与Node.js的餐饮行业AI优化方案:智能推荐与订单预测实战
人工智能·小程序·node.js
触底反弹16 小时前
🔥 前端也能玩转 AI 流式输出!从二进制流到打字机效果,一篇讲透
javascript·人工智能·node.js
海上彼尚18 小时前
Nodejs也能写Agent - 16.LangGraph篇 - 条件分支与循环
前端·后端·langchain·node.js
Goodbye1 天前
Node.js `path` 与 `fs` 模块详解——从路径操作到异步流程控制
node.js
Dreamboat-L1 天前
NVM的安装与使用(保姆级教程)
npm·node.js
拾年2751 天前
RAG 灵魂三问:你的文档是怎么"喂"给大模型的?
langchain·node.js·llm
南方程序猴1 天前
Codex CLI + VS Code 组合教程:从 Node.js 安装到接入国内中转站
gpt·ai·chatgpt·node.js·ai编程