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')
})
相关推荐
guslegend10 小时前
脚手架原理与本地调试:从 bin 软链接到 npm link
前端·npm·node.js·脚手架·前端工程化
zReadonly11 小时前
不用反复 nvm use 了:nvm-windows 2.x 按项目自动切换 Node.js
前端·node.js
百度一下吧15 小时前
Fastify 实战教程:从零搭建电商平台后台服务系统
node.js
YIAN16 小时前
LangGraph 完全入门指南:从线性工作流到带中断恢复的有状态 Agent 编排
langchain·node.js·agent
DevUp17 小时前
Directus 换掉 GPL 之后,Node.js CMS 到底怎么选?(二)
开源·node.js·cms
濮水大叔17 小时前
AI 编码实战:一句提示词,实现 CRUD、SSR 与多任务
node.js·ai编程·全栈
yume_sibai18 小时前
01-Node.js环境安装与基础
node.js
damoluomu18 小时前
Directus 换掉 GPL 之后,Node.js CMS 到底怎么选?(二)
node.js
孟陬18 小时前
JavaScript 反引号(` backtick)和双引号的不完全等价 ≠ 场景
javascript·node.js·bun