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')
})
相关推荐
不会敲代码111 小时前
手写一个「迷你 Cursor」:从零构建 AI 编程助手(LangChain + Tool Calling 实战)
langchain·node.js·cursor
不做超级小白13 小时前
把图片压小,但不糊:reduceUrImgs项目关键点拆解
前端·开源·node.js
阿赛工作室13 小时前
符合欧盟安全标准的 Node.js + Vue3 全栈架构设计
安全·node.js
uNke DEPH19 小时前
Node.js看我的就行了!!!
node.js
CV工程师(Ctrl)1 天前
OpenClaw 安装与飞书接入(2026-04-19)
node.js·github·飞书·火山引擎·openclaw
算是难了1 天前
Nestjs学习总结_3
前端·typescript·node.js
WayneYang1 天前
Node.js 全栈知识点详细整理(含代码示例 + 前端结合实战)
前端·node.js
工頁光軍1 天前
OpenAI NodeJs版使用案列
node.js·oneapi
千寻girling2 天前
被内推的面试 , 第一次
java·前端·python·面试·职场和发展·typescript·node.js
炽烈小老头2 天前
Express Routing(路由系统)
node.js·express