process-fork-call.ts
· 1.1 KiB · TypeScript
Orginalformat
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')
const child = fork(testA, {
})
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.send({ hello: 'world' })
}).addTo(app)
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000')
})
| 1 | import { App } from '@kevisual/router' |
| 2 | import { fork } from 'child_process' |
| 3 | const app = new App() |
| 4 | import path from 'path' |
| 5 | |
| 6 | // http://localhost:3000/api/router?path=call |
| 7 | app.route({ |
| 8 | path: 'call' |
| 9 | }).define(async (ctx) => { |
| 10 | ctx.body = 'Hello World' |
| 11 | const pwd = process.cwd() |
| 12 | const testA = path.join(pwd, 'src/test/a.ts') |
| 13 | const child = fork(testA, { |
| 14 | }) |
| 15 | console.log('Child process started with PID:', child.pid) |
| 16 | child.on('message', (msg) => { |
| 17 | console.log('Message from child', msg) |
| 18 | setTimeout(() => { |
| 19 | console.log('child process connected:', child.connected) |
| 20 | console.log('child process killed:', child.killed) |
| 21 | console.log('child process exit code:', child.exitCode) |
| 22 | }, 20) |
| 23 | }) |
| 24 | |
| 25 | child.on('exit', (code, signal) => { |
| 26 | console.log('子进程已退出,退出码:', code, '信号:', signal) |
| 27 | }) |
| 28 | |
| 29 | child.on('close', (code, signal) => { |
| 30 | console.log('子进程已关闭,退出码:', code, '信号:', signal) |
| 31 | }) |
| 32 | child.send({ hello: 'world' }) |
| 33 | |
| 34 | }).addTo(app) |
| 35 | |
| 36 | app.listen(3000, () => { |
| 37 | console.log('Server is running on http://localhost:3000') |
| 38 | }) |