This commit is contained in:
2025-10-15 14:47:53 +08:00
commit 943d664c36
10 changed files with 637 additions and 0 deletions

51
server/src/index.ts Normal file
View File

@@ -0,0 +1,51 @@
import { App } from '@kevisual/router'
import { fork } from 'child_process'
const app = new App()
import path from 'path'
// http://localhost:3000/api/router?path=call
app.route({
path: 'call'
}).define(async (ctx) => {
ctx.body = 'Hello World'
const pwd = process.cwd()
const testA = path.join(pwd, 'src/test/a.ts')
// 使用 Bun 的 fork 模式启动子进程
const child = fork(testA, [], {
execArgv: [],
cwd: "/tmp", // 限制工作目录
})
console.log('Child process started with PID:', child.pid)
// 监听来自子进程的消息
child.on('message', (msg) => {
console.log('Message from child:', msg)
setTimeout(() => {
console.log('child process connected:', child.connected)
console.log('child process killed:', child.killed)
console.log('child process exit code:', child.exitCode)
}, 20)
})
child.on('exit', (code, signal) => {
console.log('子进程已退出,退出码:', code, '信号:', signal)
})
child.on('close', (code, signal) => {
console.log('子进程已关闭,退出码:', code, '信号:', signal)
})
child.on('error', (error) => {
console.error('子进程错误:', error)
})
// 向子进程发送消息
child.send({ hello: 'world' })
}).addTo(app)
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000')
})