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')
})
相关推荐
摇滚侠1 天前
Node.js 零基础教程,Node.js 和 NPM 的安装与使用
前端·npm·node.js
Ashley_Amanda1 天前
Node.js 服务搭建:从零到部署的生产级指南
node.js
天远云服1 天前
Node.js实战:天远车辆出险查询API接口调用流程、代码接入与场景应用
大数据·node.js
摇滚侠1 天前
安装完 node.js 以后,需不需要修改全局安装包的目录,我觉的不需要修改。网上有很多教程让修改全局包安装目录和配置环境变量,我觉的这两步都多余。
node.js
muddjsv2 天前
Node.js 开发上手指南:从环境搭建到实战开发
node.js
福大大架构师每日一题2 天前
dify 1.11.4 正式发布:全面强化安全性、修复多项关键问题,Node.js 升级至 24.13.0!附详细升级指南
node.js·dify
winfredzhang2 天前
从零构建:基于 Node.js 与 ECharts 的量化交易策略模拟系统
前端·node.js·echarts·股票·策略
Ashley_Amanda2 天前
Node.js 版本管理指南
node.js
摇滚侠2 天前
Node.js 安装及环境变量配置,压缩包格式的安装包
node.js
天天打码3 天前
Svelte-无虚拟DOM、极致性能的现代高性能Web开发框架!
前端·node.js·vue·svelte