EXPRESS AND MONGODB
We now finally need to connect the three containers. We'll first create a simple CRUD feature in our api usingmongoose. You can go throughEasily Develop Node.js and MongoDB Apps with Mongooseto get a more detailed explanation of mongoose.
First of all, add mongoose to your express serverpackage.json
mean-docker/express-server/package.json
{
"name": "express-server",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node server.js"
},
"dependencies": {
"body-parser": "~1.15.2",
"express": "~4.14.0",
"mongoose": "^4.7.0"
}
}
We need to update our api to use mongo
mean-docker/express-server/routes/api.js
// Import dependencies
const mongoose = require('mongoose');
const express = require('express');
const router = express.Router();
// MongoDB URL from the docker-compose file
const dbHost = 'mongodb://database/mean-docker';
// Connect to mongodb
mongoose.connect(dbHost);
// create mongoose schema
const userSchema = new mongoose.Schema({
name: String,
age: Number
});
// create mongoose model
const User = mongoose.model('User', userSchema);
/* GET api listing. */
router.get('/', (req, res) => {
res.send('api works');
});
/* GET all users. */
router.get('/users', (req, res) => {
User.find({}, (err, users) => {
if (err) res.status(500).send(error)
res.status(200).json(users);
});
});
/* GET one users. */
router.get('/users/:id', (req, res) => {
User.findById(req.param.id, (err, users) => {
if (err) res.status(500).send(error)
res.status(200).json(users);
});
});
/* Create a user. */
router.post('/users', (req, res) => {
let user = new User({
name: req.body.name,
age: req.body.age
});
user.save(error => {
if (error) res.status(500).send(error);
res.status(201).json({
message: 'User created successfully'
});
});
});
module.exports = router;
Two main differences, first of all our connection to mongo db is in the lineconst dbHost = 'mongodb://database/mean-docker';. Thisdatabaseis the same as the database service we created in the docker-compose file.
We've also added rest routesGET /users,GET /users/:idandPOST /user.
Update the docker-compose file, telling the express service to link to the database service.
mean-docker/docker-compose.yml
version: '2' # specify docker-compose version
# Define the services/containers to be run
services:
angular: # name of the first service
build: angular-client # specify the directory of the Dockerfile
ports:
- "4200:4200" # specify port forewarding
express: #name of the second service
build: express-server # specify the directory of the Dockerfile
ports:
- "3000:3000" #specify ports forewarding
links:
- database # link this service to the database service
database: # name of the third service
image: mongo # specify image to build container from
ports:
- "27017:27017" # specify port forewarding
Thelinksproperty of the docker-compose file creates a connection to the other service with the name of the service as the host name. In this casedatabasewill be the hostname. Meaning, to connect to it frrom theexpressservice, we should usedatabase:27017. That's why we made thedbHostequal tomongodb://database/mean-docker.