需要从另一个 NodeJs 应用程序 运行 一个 NodeJs 应用程序
Need to run a NodeJs application from another NodeJs application
我在以下目录中有一个 NodeJs 应用程序 运行ning
第一个应用程序的路径 '/users/user1/projects/sampleProject' 运行ning 在 3000 端口。
第二个应用程序的路径 '/users/user1/demoProjects/demo1' 将在 5000 端口 运行 触发第一个应用程序的路由器功能。
第二个 NodeJs 应用程序尚未启动(它将在端口 5000 运行)。它需要 运行 独立地在第一个 NodeJs 应用程序中点击路由器功能,该应用程序在端口 3000 上 运行ning,即(http://localhost:3000/server/startServer)。我是 NodeJs 子进程的新手,如果我错了,请纠正我。并建议我一个正确的方法来做到这一点。谢谢
Start another node application using node.js?
我试过如下
// First NodeJs application
import { exec } from "child_process";
router.get('/startServer', async (req, res, next) => {
console.log("Initiated request")
let startServerInstance = 'cd "/users/user1/demoProjects/demo1" && npm run dev'; // path for the second NodeJs application
console.log("Server instance path => " + startServerInstance)
try {
// exec from child process, Spawns a shell then executes the command within that shell
let child = exec(startServerInstance, function (err, stdout, stderr) {
if (err) throw err;
else {
console.log("result ")
res.json({
status: 'success'
});
}
});
} catch (error) {
res.json({
status: 'error',
message: error
});
}
});
上面的代码执行命令并在后台触发第二个应用程序 运行 但它没有 return 任何东西。错误或成功结果。
您需要使用stout
和stderror
查看其他服务器日志。你的代码也不正确。如果您使用 if
而不使用 {}
,它将不会转到 else
语句。这就是为什么您在控制台中看不到 'result' 文本的原因。
import {
exec
} from "child_process";
router.get('/startServer', async (req, res, next) => {
console.log("Initiated request")
let startServerInstance = 'cd "/users/user1/demoProjects/demo1" && npm run dev'; // path for the second NodeJs application
console.log("Server instance path => " + startServerInstance)
try {
// exec from child process, Spawns a shell then executes the command within that shell
let child = exec(startServerInstance, function(err) {
if (err) throw err;
console.log("Server started");
});
child.stdout.on('data', (data) => {
// this is new server output
console.log(data.toString());
});
child.stderr.on('data', (data) => {
// this is new server error output
console.log(data.toString());
});
res.json({
status: 'success'
});
} catch (error) {
res.json({
status: 'error',
message: error
});
}
});
子进程回调仅在进程终止时调用。如果进程保持运行,不触发回调。
我在以下目录中有一个 NodeJs 应用程序 运行ning
第一个应用程序的路径 '/users/user1/projects/sampleProject' 运行ning 在 3000 端口。
第二个应用程序的路径 '/users/user1/demoProjects/demo1' 将在 5000 端口 运行 触发第一个应用程序的路由器功能。
第二个 NodeJs 应用程序尚未启动(它将在端口 5000 运行)。它需要 运行 独立地在第一个 NodeJs 应用程序中点击路由器功能,该应用程序在端口 3000 上 运行ning,即(http://localhost:3000/server/startServer)。我是 NodeJs 子进程的新手,如果我错了,请纠正我。并建议我一个正确的方法来做到这一点。谢谢
Start another node application using node.js?
我试过如下
// First NodeJs application
import { exec } from "child_process";
router.get('/startServer', async (req, res, next) => {
console.log("Initiated request")
let startServerInstance = 'cd "/users/user1/demoProjects/demo1" && npm run dev'; // path for the second NodeJs application
console.log("Server instance path => " + startServerInstance)
try {
// exec from child process, Spawns a shell then executes the command within that shell
let child = exec(startServerInstance, function (err, stdout, stderr) {
if (err) throw err;
else {
console.log("result ")
res.json({
status: 'success'
});
}
});
} catch (error) {
res.json({
status: 'error',
message: error
});
}
});
上面的代码执行命令并在后台触发第二个应用程序 运行 但它没有 return 任何东西。错误或成功结果。
您需要使用stout
和stderror
查看其他服务器日志。你的代码也不正确。如果您使用 if
而不使用 {}
,它将不会转到 else
语句。这就是为什么您在控制台中看不到 'result' 文本的原因。
import {
exec
} from "child_process";
router.get('/startServer', async (req, res, next) => {
console.log("Initiated request")
let startServerInstance = 'cd "/users/user1/demoProjects/demo1" && npm run dev'; // path for the second NodeJs application
console.log("Server instance path => " + startServerInstance)
try {
// exec from child process, Spawns a shell then executes the command within that shell
let child = exec(startServerInstance, function(err) {
if (err) throw err;
console.log("Server started");
});
child.stdout.on('data', (data) => {
// this is new server output
console.log(data.toString());
});
child.stderr.on('data', (data) => {
// this is new server error output
console.log(data.toString());
});
res.json({
status: 'success'
});
} catch (error) {
res.json({
status: 'error',
message: error
});
}
});
子进程回调仅在进程终止时调用。如果进程保持运行,不触发回调。