NodeJS start mongoDB server - node.js

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

Related

express.js run endpoint from the command line

I have an express back end and mongodb database. I want to run a maintenance task to do some deletion of stale data in the db. The easiest and quickest way I can think to do this is use my existing code (database connection, app.js with all of the express setup, etc.) and setup a new route (ex. maintenance.js) that performs the task.
My question is how to hit the endpoint from the command line? Just running node maintenance.js won't actually cause the endpoint to execute unless I'm mistaken. My understanding is it will just make the endpoint accessible on the port the process is running on. My thought is maybe I serve the endpoint in one command line and run some command to hit it from another command line tab? Appreciate the help!
You can use mongo database connection string to connect to database using Mongo shell.
mongo <URI_string>
After the connection gets established. Use shell commands for query and deletion.
https://docs.mongodb.com/manual/reference/connection-string/

Express.js app stops without PM2 restarting it

I have an Express JS app that is run using PM2 behind a NGINX proxy. It works fine, except for when we send a push notification to all users and the server probably gets too much load. The problem though is that the app just hangs when this happens. Nginx logs reports Connection Refused, Timeouts. My express app doesn't report any errors when this happens so I can't really find what is happening when it hangs. PM2 doesn't restart the app automatically.
It kind of feels like there is a promise that's not getting resolved or rejected, but I have no proof of this. It's just a feeling. Also, should this be logged somewhere? Node usually warns that this will be different in the future.
I know my description is generic, but does anyone have any experience in similar problems? Or maybe can guide me how to find out more why it breaks?
Node 12,
Express 4,
Postgres 9,
pg-promise,
Nginx,
Ubuntu Linux

Where should a mongodb process be started for a node application

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).

Sails mongodb missing connection handling

Very simple and stupid question I came up with today when was playing around with sails and mongo db (adapter of the waterline odm). I just shut down the mongo while my sails server still has been running and saw nothing, neither in logs of sails or in the browser. How I could handle such situation?
If sails.js attempts a connection to the DB you will most certainly get a message in your console if the Mongo server is shut down, but if no request is made that requires a DB connection, then you will not see any error because no error as of yet exists.
It sounds like you might require a process that will monitor your DB and check to make sure it is still running? There are Sail.js options where you could create a CRON job to check the connection. Or you could use other application monitoring services like New Relic to monitor your DB.

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.

Resources