Demonstration of how to integrate multiple mongodb instances in a nodejs + express app and how we can provide different mongodb database for different hosts.
5. Freelancer Remedies
How to use multiple mongodb databases in a nodejs express mongodb application
Presentation content
1. Describe project
2. Live Demo
3. Explain code
4. Share git repo link
6. Freelancer Remedies
How to use multiple mongodb databases in a nodejs express mongodb application
1. Describe project
Single express node js application need to serve two hosts.
Routes and models are the same but the mongodb database are different.
7. Freelancer Remedies
How to use multiple mongodb databases in a nodejs express mongodb application
1. Describe project
Single express node js application need to serve two hosts.Routes and
models are the same but the mongodb database are different.
For example:
Node Js
Express Js
Mongoose
DB
DB
Host 1
Host 2
Application
mongodb://localhost/localhost-dev
mongodb://localhost/physical-dev-127
http://localhost:3000
http://127.0.0.1:3000
Routes
/ -- showing user json data from the database
/random -- insert random strings to user table
Available routes
1. http://localhost:3000/
2. http://locahost:3000/random
Available routes
1. http://127.0.0.1:3000/
2. http://127.0.0.1::3000/random
Master Server
Hosts pointed to the master server
10. Freelancer Remedies
How to use multiple mongodb databases in a nodejs express mongodb application
Explain Code
11. Freelancer Remedies
How to use multiple mongodb databases in a nodejs express mongodb application
Code 1:
Routes
Creating new user model
related to the current requested
hostname
Using user model
related to the current requested
hostname
Route definitions and Starting the server code
12. Freelancer Remedies
How to use multiple mongodb databases in a nodejs express mongodb application
Code 2:
User schema
Here is the middleware code
for saving the mongodb instance by host
name to the global stack
Defining the global Stack
13. Freelancer Remedies
How to use multiple mongodb databases in a nodejs express mongodb application
Git Repository:
https://github.com/phpremedy/Simple-Hack-Use-multipple-mongodb-databases-in-a-nodejs-express-mongodb-application