socket.io 服务器 Node.js 和 esbuild
我正在尝试使用 socket.io 设置一个服务器(API),并在它们之间连接一个客户端,这是我的 esbuild 配置,使用 esbuild-serve
进行热重载(客户端在端口 7000 上运行,并且BE on 8000)
import esbuildServe from 'esbuild-serve'
esbuildServe(
{
logLevel: 'info',
entryPoints: ['src/index.js'],
// platform: 'node',
// target: ['node16.13'],
// format: 'cjs',
bundle: true,
outfile: 'dist/main.js',
external: ['socket.io'],
sourcemap: true,
minify: true
},
{
port: 8000,
root: 'dist'
}
)
这是我的项目树,Docker 只需使用 Node 16 基础镜像并运行命令 node esbuild.config.js
来启动 dev 热重载服务器并实例化2 个 node.js 图像公开 7000(客户端)、8000(BE)、8001(websocket 端口)
客户端代码似乎工作正常:
import { io } from 'socket.io-client'
const socket = io('localhost:8001')
socket.on('connect', (socket) => {
console.log(socket)
})
socket.on('message', function (data) {
console.log('message', data.message)
})
这是 BE 代码似乎没有正确捆绑
// import { createServer } from 'http'
import { Server } from 'socket.io'
// const httpServer = createServer()
// const io = new Server(httpServer, {
const io = new Server({
port: 8001,
cors: {
origin: '*'
}
})
io.on('connection', function (socket) {
socket.emit('message', { message: 'A new user has joined!' })
})
// httpServer.listen(8001)
// io.emit('connection', (socket) => {
// return socket
// })
通过删除 external: ['socket.io'],
它开始抱怨 node_module 缺少依赖项,因此我按照建议启用了这些设置
platform: 'node',
target: ['node16.13'],
format: 'cjs',
:加载 localhost:8000 后的版本我遇到此错误 Uncaught ReferenceError: require is not Defined
更新:
我已将格式 cjs
更改为 esm
现在似乎编译但没有抛出这个新错误:
Uncaught TypeError: 无法解析模块说明符“socket.io”。相对引用必须以“/”、“./”或“../”开头
I'm trying to setup a server (API) with socket.io and a client connected between them, this is my esbuild config using hot-reload with esbuild-serve
(client it's running on port 7000 and BE on 8000)
import esbuildServe from 'esbuild-serve'
esbuildServe(
{
logLevel: 'info',
entryPoints: ['src/index.js'],
// platform: 'node',
// target: ['node16.13'],
// format: 'cjs',
bundle: true,
outfile: 'dist/main.js',
external: ['socket.io'],
sourcemap: true,
minify: true
},
{
port: 8000,
root: 'dist'
}
)
and this is my project tree, Docker simply use a node 16 base image and run the command node esbuild.config.js
to start dev hot-reload server and instantiate the 2 node.js images exposing 7000(client), 8000(BE), 8001(websocket port)
Client code seems to work OK:
import { io } from 'socket.io-client'
const socket = io('localhost:8001')
socket.on('connect', (socket) => {
console.log(socket)
})
socket.on('message', function (data) {
console.log('message', data.message)
})
and this is the BE code that doesn't seem to be bundled correctly
// import { createServer } from 'http'
import { Server } from 'socket.io'
// const httpServer = createServer()
// const io = new Server(httpServer, {
const io = new Server({
port: 8001,
cors: {
origin: '*'
}
})
io.on('connection', function (socket) {
socket.emit('message', { message: 'A new user has joined!' })
})
// httpServer.listen(8001)
// io.emit('connection', (socket) => {
// return socket
// })
By removing the external: ['socket.io'],
it start complaining about missing dependencies from the node_module so I enable these settings as it suggest:
platform: 'node',
target: ['node16.13'],
format: 'cjs',
But then the compiled version once I load localhost:8000 I have this error Uncaught ReferenceError: require is not defined
UPDATE:
I've changed the format cjs
to esm
and now seems to compile but not it's throwing this new error:
Uncaught TypeError: Failed to resolve module specifier "socket.io". Relative references must start with either "/", "./", or "../"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论