Nodejs exec mongodb command in Bluebird Promise

2019-05-28 11:23发布

问题:

I want to run mongod command in node.js using Promises, so that database operations can run only after mongodb process is started. I tried my hands with following, but failed:

var Promise = require("bluebird");

var execAsync = Promise.promisify(require('child_process').exec);

execAsync("~/mongodb/bin/mongod").then(function(result){
    console.log("started mongodb...");
}).catch(function(error){
    console.log("error in starting mongodb..."+JSON.stringify(error));
});

Any suggestions?

回答1:

You shouldn't start your mongod process in node, you should be doing it some other way. Then you can just check whether it's running, not whether it has started up properly.