Node/Mongo API in a Container, based on:
-
If node is already installed manually (not using brew) uninstall as per https://gist.github.com/TonyMtz/d75101d9bdf764c890ef
After uninstalling and reinstalling node i had issues with npm which I fixed by cleaning everything up.
-
Install with brew (if not already) by running
brew install nodeI ran into some issues, resolved with help from Stackoverflow.
-
Install mongodb with brew
brew install mongodb -
Create directory needed by mongo process (mongod) to write data
mkdir -p /data/db -
Change ownership so mongod can write to it
sudo chown -R luisweir:staff /data -
Run mongo
mongod
-
Start mongo DB
mongod -
Open a new terminal and run command to open mongo CLI
mongo -
To create a new DB run
> use orders_db > show dbs > a = { "order" : { "order_id" : "1073459962", "status" : "created", "created_at" : "2016-04-01T12:00:10-02:00", "updated_at" : "2016-06-06T15:48:20-04:00", "total_price" : 50, "currency" : "EUR", "customer" : { "customer_id" : "76422323213", "first_name" : "Paul", "last_name" : "Norman", "phone" : "+44 (0) 5555-555-555", "email" : "paul.norman@example.com" }, "address" : [ { "type" : "billing, shipping", "city" : "Cambridge", "county" : "Cambridgeshire", "postcode" : "CB4 4SS", "country" : "Great Britain" } ], "line_items" : [ { "line_id" : "1245555432", "product_id" : "1071823172", "product_name" : "Billy Table White", "quantity" : 1, "price" : 50, "currency" : "EUR", "grams" : 8000, "sku" : "BILLY2009WHITE" } ] } } > db.orders.insert() > show dbs > db.orders.find() > db.orders.find({ "_id" : ObjectId("578639323179eba600803d19") })Note:
.save()can also be used which is basically an upsert.
-
Create project folder
mkdir <path>/<project> ie. mkdir -p $HOME/node/orders_api -
Cd into project folder (ie.
$HOME/node/first_api) and create Node project by running:npm initThis will create a
package.json, in my example like this:{ "name": “oders_system_api”, "version": "1.0.0", "description": “IKEA orders system API“, "main": "server.js", "scripts": { "test": "test" }, "repository": { "type": "git", "url": "git+https://github.com/luisw19/node.git" }, "keywords": [ "Node.js", "git" ], "author": "Luis Weir", "license": "ISC", "bugs": { "url": "https://github.com/luisw19/node/issues" }, "homepage": "https://github.com/luisw19/node#readme" }
-
Install express locally on project (more info)
npm install --save express -
Install mongoose in project (more info)
npm install --save mongoose(the
--saveis so mongoose is added to the dependencies in thepackage.json- otherwise is just temporarily added) -
Install body-parser
npm install --save body-parser -
Install modules by running (note sure why is this required??)
npm install
-
Create
server.jsand add basic codevar express = require("express"); var app = express(); var bodyParser = require("body-parser"); var router = express.Router(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({"extended" : false})); router.get("/",function(req,res){ res.json({"error" : false,"message" : "Hello World"}); }); app.use('/',router); app.listen(3000); console.log("Listening to PORT 3000"); -
Run to test node app
npm startOpen a new terminal and test URL with curl (or use postman or simply use the browser)
curl http://localhost:3000
-
In project folder create a new folder called
modelmkdir model -
Inside the model folder create
mongo.jstouch mongo.js -
Add code accordingly. Of most important is that the mongoose schema is done properly… (see file in project)
-
Back in root project folder add the API code to
server.js
Create docker container for API (based on https://nodejs.org/en/docs/guides/nodejs-docker-webapp/)
-
Install docker. On a Mac use
brew cask:brew cask install dockerAlternatively, docker can be downloaded from the official website.
-
Create a docker file inside the node application directory
touch Dockerfile -
Edit the file as following:
FROM node:argon # Create app directory RUN mkdir -p /usr/src/app WORKDIR /usr/src/app # Install app dependencies COPY package.json /usr/src/app/ RUN npm install # Bundle app source COPY . /usr/src/app EXPOSE 8080 CMD [ "npm", "start" ] -
Build the container
docker build -t luisw19/orders_api -
Push the docker image to docker hub
docker login --username=<username> docker push <image name>
-
Install docker compose (or downloaded from the official page)
brew install docker-compose -
Create docker compose file in the parent directory
touch docker_compose.yml -
Add content as following:
version: '2' services: order_api: #Uncomment next line if you wish to build from docker-compose #build: ./orders_api image: luisw19/orders_api depends_on: - mongo_db ports: - "3000:3000" command: npm start links: - mongo_db mongo_db: image: mongo:3.0.2 -
Build the container (only if building from source code - otherwise the next step is all needed to run)
docker-compose build -
Create and start the containers
docker-compose upAfter the initial create,
docker-compose start / stopcan be used instead. -
Test the API using the postman collection:
orders_api/ordersSystemAPI.postman_collection