Jeeva
Jeeva

Reputation: 1590

Need to run a NodeJs application from another NodeJs application

I have a NodeJs application running in the following directory

First Application's Path '/users/user1/projects/sampleProject' which is running at 3000 port.

Second Application's Path '/users/user1/demoProjects/demo1' which is going to run at 5000 port on triggering the router function from first application.

The second NodeJs application is not yet started(It will run at port 5000). It need to run independently on hitting a router function in the first NodeJs Application which is running on port 3000 ie(http://localhost:3000/server/startServer). I'm new to NodeJs child processes, Kindly correct me if i'm wrong. And suggest me a right way to do it. Thanks

Start another node application using node.js?

I have tried it like below

// 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
        });
    }    
});

The above code executes the command and triggered the second application to run in background but it doesn't return anything. Either error or success result.

Upvotes: 1

Views: 1505

Answers (2)

hurricane
hurricane

Reputation: 6724

You need to use stout and stderror to check other server logs. Also your code is not correct. If you use if without {} it will not go to else statement. That is why you don't see 'result' text in console.

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
        });
    }
});

Upvotes: 2

kvnam
kvnam

Reputation: 1455

Child process callback is only called once the process terminates. If the process keeps running, callback is not triggered.

Explained here - https://nodejs.org/docs/latest-v10.x/api/child_process.html#child_process_child_process_exec_command_options_callback

Upvotes: 1

Related Questions