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')
})
相关推荐
None3211 天前
【NestJs】基于Redlock装饰器分布式锁设计与实现
后端·node.js
Gogo11211 天前
构建高性能 Node.js 集中式日志体系 (下篇):Pino + PM2 + OpenSearch 代码落地实战
node.js
小岛前端1 天前
Node.js 宣布重大调整,运行十年的规则要改了!
前端·node.js
前端付豪1 天前
Nest 项目小实践之前端注册登陆
前端·node.js·nestjs
codingWhat2 天前
整理「祖传」代码,就是在开发脚手架?
前端·javascript·node.js
ServBay2 天前
Node.js、Bun 与 Deno,2026 年后端运行时选择指南
node.js·deno·bun
码路飞2 天前
Node.js 中间层我维护了两年,这周终于摊牌了——成本账单算完我人傻了
node.js
None3213 天前
【NestJs】使用Winston+ELK分布式链路追踪日志采集
javascript·node.js
Dilettante2583 天前
这一招让 Node 后端服务启动速度提升 75%!
typescript·node.js
Mr_li4 天前
NestJS 集成 TypeORM 的最优解
node.js·nestjs