Where should a mongodb process be started for a node application - node.js

I want my application server(Hapi/Express) to start the mongodb process before proceeding with server.start(). A good way to do this is via Promises so that the mongod return code can be captured in .then and checked for start success/failure.
I posted a similar question # Nodejs exec mongodb command in Bluebird Promise, that prompted me to ask this one here.

You seem to not understand the basics of processes.
so that the mongod return code can be captured in .then
mongod will not return any code until it exits (it's called "exit code" for a reason). I assume that you want your mongodb running, so this means no code for you.
Starting a database server from the application is absolutely the wrong way to do it. Database and application should be started separately (by OS' startup manager or whatever). If you install mongodb from a package, the auto-startup should be handled for you (via installing proper init script).
Application should only know a connection string (and if it can't connect to the database at this string, show some pretty error message).

Related

NodeJS start mongoDB server

I need to start the mongoDB server from my NodeJS application. I managed to do this before but for some reason I forgot how. I though I used a cild process but not sure anymore as I can't get anything to work at the moment.
How would I start the mongoDB server (command mongod) from withing my NodeJS app and execute some other code when the server had been started (guessing using a promise...)?
You can use child_process to run mongod from your application, but this may cause the MongoDB server to exit when your app exits. It's generally better to have the DB server running all the time.
https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

Node JS persist promises upon restart (maybe with versioning)

Imagine building a bot, that handles dialogs as promise chains in nodejs
bot.ask('question')
.then(answer => { ...someDataAndCode })
.then(somethingElse)
.then(etc);
Now, upon deploying the new version, all the promises are gone, of course, and then you have hung dialogs where bot doesn't reply. The questions are:
Is there any npm package/approach that would persist promises between restarts?
In case logic of that persisted promise updates, is there package that would keep persisted promise old version, so that "old dialogs go the old way"?
I am not familiar with any persistent promise module, and I think it would be very difficult to keep the state of each chained promise....
But any way, I would follow another approach to your problem. I would not kill the previous server, but I would run the new updated version in a different port. Then I would configure the reverse proxy (I assume you use something like nginx to route the traffic to node) to send all the new users to the new version of the node server.
When the old node server has finished its work and even the last user has gone, then you can kill that server.
P.S All of this depends on the assumption that you are using a reverse-proxy, something you don't state in the question, so my solution could not fit in your project

NodeJS Not Exiting, How to find open handlers?

I have a fairly simple NodeJS script that is not exiting gracefully when done (this is a worker and I want to fire it up using a cron job / heroku scheduler)
Once it has finished its task it just sits there waiting. It doesn't use express / a web server etc and as far as I can tell I have resolved all callbacks.
Is there some way to tell what node is still working on / what handlers are open?
My imported libraries are
Request, Q & Mongoose.
You definitely need to close the mongodb connections that mongoose has open on your behalf by calling mongoose.disconnect(). Have you tried that?

restart nodejs server programmatically

User case:
My nodejs server start with a configuration wizard that allow user to change the port and scheme. Even more, update the express routes
Question:
Is it possible to apply the such kind of configuration changes on the fly? restart the server can definitely bring all the changes online but i'm not sure how to trigger it from code.
Changing core configuration on the fly is rarely practiced. Node.js and most http frameworks do not support it neither at this point.
Modifying configuration and then restarting the server is completley valid solution and I suggest you to use it.
To restart server programatically you have to execute logics outside of the node.js, so that this process can continue once node.js process is killed. Granted you are running node.js server on Linux, the Bash script sounds like the best tool available for you.
Implementation will look something like this:
Client presses a switch somewhere on your site powered by node.js
Node.js then executes some JavaScript code which instructs your OS to execute some bash script, lets say it is script.sh
script.sh restarts node.js
Done
If any of the steps is difficult, ask about it. Though step 1 is something you are likely handling yourself already.
I know this question was asked a long time ago but since I ran into this problem I will share what I ended up doing.
For my problem I needed to restart the server since the user is allowed to change the port on their website. What I ended up doing is wrapping the whole server creation (https.createServer/server.listen) into a function called startServer(port). I would call this function at the end of the file with a default port. The user would change port by accessing endpoint /changePort?port=3000. That endpoint would call another function called restartServer(server,res,port) which would then call the startServer(port) with the new port then redirect user to that new site with the new port.
Much better than restarting the whole nodejs process.

Running arbitrary MongoDB scripts through Node.js driver

I have a combination of complex MongoDB scripts that runs from the command line as follows:
$ mongo mydb config.js task.js
Since I can't run shell scripts in my server environment and need to schedule the above task, I figured I could simply concatenate the above .js files and then run them from a Node script. Hence I am looking for an equavalent to:
db.runMyCustomRawCommands(string commands)
How can I do this, or what would be an alternative solution?
To quote Christian Kvalheim, original author of the Node.js native MongoDB driver:
that's not possible as the shell is synchronous and have different apis than the node.js driver. you would have to rewrite the scripts to work for node.js.
The problem is that db.runMyCustomRawCommands is not a raw command. The drivers communicate with the mongod server on a lower level. Commands such as db.abc you run in the console are actually simple query messages referencing the db.$cmd collection as db.$cmd.findOne({ abc: 1 }) or similarly.
You thus have to either figure out how to express your mongo shell script as calls to your driver's API or access the server on a lower level.

Resources