first commit
This commit is contained in:
46
start-production.cjs
Normal file
46
start-production.cjs
Normal file
@@ -0,0 +1,46 @@
|
||||
const { spawn } = require('child_process');
|
||||
const path = require('path');
|
||||
|
||||
// 生产环境配置
|
||||
const PORT = process.env.PORT || 4001;
|
||||
const NODE_ENV = 'production';
|
||||
|
||||
// 设置环境变量
|
||||
process.env.NODE_ENV = NODE_ENV;
|
||||
process.env.PORT = PORT;
|
||||
|
||||
console.log(`启动生产环境服务器,端口: ${PORT}`);
|
||||
console.log(`工作目录: ${process.cwd()}`);
|
||||
|
||||
// 启动后端服务器
|
||||
const server = spawn('node', ['--import', 'tsx/esm', 'api/server.ts'], {
|
||||
stdio: 'inherit',
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_ENV: NODE_ENV,
|
||||
PORT: PORT
|
||||
}
|
||||
});
|
||||
|
||||
// 处理进程退出
|
||||
server.on('close', (code) => {
|
||||
console.log(`服务器进程退出,退出码: ${code}`);
|
||||
process.exit(code);
|
||||
});
|
||||
|
||||
// 处理错误
|
||||
server.on('error', (err) => {
|
||||
console.error('启动服务器时发生错误:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// 优雅关闭
|
||||
process.on('SIGINT', () => {
|
||||
console.log('\n收到 SIGINT 信号,正在关闭服务器...');
|
||||
server.kill('SIGINT');
|
||||
});
|
||||
|
||||
process.on('SIGTERM', () => {
|
||||
console.log('\n收到 SIGTERM 信号,正在关闭服务器...');
|
||||
server.kill('SIGTERM');
|
||||
});
|
||||
Reference in New Issue
Block a user