19

I have an app which runs on express and communicates with mongodb. This is how I start my app:

1.Start Mongodb

mongod --dbpath data --config mongo.conf"

2.Start Express

node server.js

My question is, Is there a way to combine these? I know node is single threaded so we cant run both express and mongo from server.js but what is the correct way? Is it possible to start mongo from a javascript file using npm?

Edit:

I can run mongod --dbpath data and node server.js separately on two different command prompt. My question is to start them from one file (if possible).

WhatisSober
  • 988
  • 2
  • 12
  • 32

6 Answers6

19

start creates new cmd in windows here is my config:

"scripts": {
  "prestart": "start mongod --config ./data/mongod.cfg",
  "start": "node ./server/bin/www",
  "poststart": "start mongo admin --eval \"db.getSiblingDB('admin').shutdownServer()\"",
  "pretest": "start mongod --dbpath data",
  "test": "mocha test",
  "posttest": "start mongo admin --eval \"db.getSiblingDB('admin').shutdownServer()\""
},

Good luck!

Peter
  • 1,742
  • 15
  • 26
  • 1
    For anyone else in the same situation, I had my mongo folder in a higher sibling folder, so I changed my config to look like this `"prestart": "cd .. & md mongo-db & start mongod --dbpath ./mongo-db"` – dano Oct 08 '17 at 18:02
  • 2
    @Peter Its not exactly clear from this answer exactly the best way to start Mongo, then Node. What exactly are you typing in your terminal to start things up? (i.e. is `prestart` a reserved command that automatically runs when you type `npm start` on the command line?) How do you use these scripts? These scripts are contained within the `package.json` file, correct? – zipzit Oct 22 '18 at 09:21
  • 1
    @zipzit His question is not about the "best way" (title is miss leading), hes question is: `I can run mongod --dbpath data and node server.js separately on two different command prompt. My question is to start them from one file (if possible).` – Peter Oct 25 '18 at 20:21
3

In your package.json you can defined scripts. Here is a list of reserved commands can be found here: https://docs.npmjs.com/misc/scripts

If you are on a based OS unix you can do something like this:

"scripts": {
    "prestart": "mongod --dbpath data --config mongo.conf &",
    "start": "node server.js",
    "poststart": "kill %%",
}

Then when you want to run this from the terminal just do npm start

The & at the end of the prestart command means run in background and the kill %% in the poststart command kills the most resent background task (you can also do %1 for the first background task). This might break if you have other background tasks going so be aware of that.

Also if you are hosting MongoDB on another server for production but locally for development you can use:

"scripts": {
    "start": "node server.js",
    "predev": "mongod --dbpath data --config mongo.conf &",
    "dev": "node server.js",
    "postdev": "kill %%",
}

Then when you want to do development you can use npm dev and when you are in production you can use npm start.

Also keep in mind when you are setting up your MongoClient to specify useNewUrlParser: true and useUnifiedTopology: true in the options argument for MongoClient.connect(url, opts) because mongoDB has a small startup time and more likely then not your node script is going to have a smaller startup time then your database and will through an error saying your database was not found.

Leyla Becker
  • 58
  • 1
  • 5
  • I like this solution (as I am on Linux), but `poststart`/`postdev` should be changed to `mongo admin --eval 'db.shutdownServer()' &> /dev/null`. Note that I redirect the output to `/dev/null`, because it throws an error when I run it like this (there are no errors when run in Mongo Shell), but it works. Also note that I am forking the server, therefore I don’t background the `prestart`/`dev` command. – tukusejssirs Apr 01 '21 at 13:43
2

Set-up mongo to run as a windows service, I always have mongo on and it has worked for the last 3 years on my dev machine. On deployment machine set mongo up to be controlled by a daemon.

simon-p-r
  • 3,623
  • 2
  • 20
  • 35
1

If you are running on Linux you could use the package.json file to define scripts which do just what you need.

There are a few issues altho :

If you are running Linux you could use

"mongod --fork --dbpath data --config mongo.conf" and "node index.js" to use mongodb and run the app at the same time and that would work just fine.

But if you are on windows you have to use a separate console window for mongo and a separate one for the app.

If you are running on Windows I would probably use my package.json scripts to run mongodb and I would run my app in another terminal since it's easier to type node index.js than the mongod part.

Christo S. Christov
  • 2,268
  • 3
  • 32
  • 57
  • Thank you Chris. Is there a way to stat to do "mongod --dbpath data" from a javascript file sort of like node startdatabase.js. – WhatisSober Oct 24 '15 at 10:14
  • @WhatisSober You have to run a process with command line arguments in that case which I think i more tedious than configuring the package.json file and running a separate window for the app. It's just sad that Windows doesn't offer the --fork flag. – Christo S. Christov Oct 24 '15 at 10:18
0

try this.

"scripts": {
"prestart": "start  \"mongoServer\" \"c:Program Files/MongoDb/Server/3.4/bin/mongod.exe\" &    start \"redis\" \"c:Program Files/Redis/redis-server.exe\"",
"start": "node app.js",
"stop": "Taskkill /IM mongod.exe  & Taskkill /IM redis-server.exe"},
EAS
  • 366
  • 2
  • 18
-1

Yes. Using your package.json you can define scripts:

{
  "name": "my package",
  "version": "1.0.0",
  "scripts": {
    "start": "mongod --dbpath data --config mongo.conf && node server.js",
  },
  "devDependencies": {
    "express": "*"
  }
}

Calling npm start will execute your start script defined in the package.json

For more see https://docs.npmjs.com/misc/scripts

drowzy
  • 657
  • 6
  • 9