express.js run endpoint from the command line - node.js

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/

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

use custom mongoDB server instead of mongolab

Today i started migrating from firebase to mongoDB,
I used this tutorial and its all up and running http://thejackalofjavascript.com/re-architecting-a-firebase-app-in-node/
When checking the code i see there is a mongolab link connected to the code,
mongodb://admin:admin123#ds061620.mongolab.com:61620/testsync
My question is: can i easily setup my own local database to use instead of this? and what packages i would need to do this?
The main reason for switching to mongo instead of firebase is the pricing, please take this into account.
Yes, of course you can.
First, install mongodb locally, to do that, follow the instructions for the distribution you're working on.
Then, make sure your mongodb service is running.
After that, on your database connection script, change the connection parameters.
I guess that you have something like this on your .js file:
mongodb://admin:admin123#ds061620.mongolab.com:61620/testsync
Just to make a connection test, try to change it to:
mongo://localhost/test
After a successful connection, you can start to manage your database as you want.
As additional information, you don't have to specify user, password and a different port than the mongo's default, because you're using your local configuration, if you want to do so, you have to configure your mongodb server to make it work that way.
I'm working with mongoose ORM to manage a local database, and here's my connection function working.
Mongoose connection to local database
Hope this helps you.

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

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