add init demos
This commit is contained in:
		@@ -1,51 +1,16 @@
 | 
			
		||||
import { App } from '@kevisual/router'
 | 
			
		||||
import { fork } from 'child_process'
 | 
			
		||||
const app = new App()
 | 
			
		||||
import path from 'path'
 | 
			
		||||
import { proxyRoute, initProxy } from '@kevisual/local-proxy/proxy.ts';
 | 
			
		||||
// http://localhost:4005/test/a/index.html
 | 
			
		||||
initProxy({
 | 
			
		||||
  pagesDir: './demo',
 | 
			
		||||
  watch: true,
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// 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')
 | 
			
		||||
import { app } from './app.ts'
 | 
			
		||||
import './routes/index.ts'
 | 
			
		||||
 | 
			
		||||
  // 使用 Bun 的 fork 模式启动子进程
 | 
			
		||||
  const child = fork(testA, [], {
 | 
			
		||||
    execArgv: [],
 | 
			
		||||
    cwd: "/tmp", // 限制工作目录
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  console.log('Child process started with PID:', child.pid)
 | 
			
		||||
app.listen(4005, () => {
 | 
			
		||||
  console.log('Server is running on http://localhost:4005')
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
  // 监听来自子进程的消息
 | 
			
		||||
  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')
 | 
			
		||||
})
 | 
			
		||||
app.onServerRequest(proxyRoute);
 | 
			
		||||
		Reference in New Issue
	
	Block a user