Correct way of starting mongodb and express?

2020-05-25 07:49发布

问题:

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

回答1:

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!



回答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.



回答3:

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.



回答4:

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",
    "pretest": "mongod --dbpath data --config mongo.conf &",
    "test": "node server.js",
    "posttest": "kill %%",
}

Then when you want to do development you can use npm test 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.



回答5:

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"},


回答6:

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