├── .env ├── .nvmrc ├── .eslintrc.json ├── .gitignore ├── tests └── sampleTest.js ├── . dockerignore ├── process.yml ├── README.md ├── src ├── server.js ├── services │ └── BookService.js ├── models │ └── Book.js ├── app.js ├── routes │ └── BookRoutes.js ├── db │ └── DBConnection.js └── controller │ └── BookController.js ├── docker-compose.yml ├── package.json ├── Dockerfile └── LICENSE /.env: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | 10.15.3 -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /tests/sampleTest.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /. dockerignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log -------------------------------------------------------------------------------- /process.yml: -------------------------------------------------------------------------------- 1 | apps: 2 | - script : 'src/server.js' 3 | name : 'bookshop-api' 4 | exec_mode: 'cluster' 5 | instances: 2 6 | watch : false -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NodeJS - Example Microservice Project Structure 2 | 3 | ## To run project execute below two commands 4 | - docker-compose build 5 | - docker-compose up 6 | -------------------------------------------------------------------------------- /src/server.js: -------------------------------------------------------------------------------- 1 | const app = require("./app"); 2 | const db = require("./db/DBConnection"); 3 | 4 | app.set("port", process.env.PORT || 8080); 5 | 6 | app.listen(app.get("port"), () => { 7 | console.log("Express server started and running!"); 8 | }); 9 | -------------------------------------------------------------------------------- /src/services/BookService.js: -------------------------------------------------------------------------------- 1 | const Book = require("../models/Book"); 2 | 3 | function BookService() { 4 | return { 5 | list: () => Book.find(), 6 | add: data => new Book(data).save(), 7 | delete: id => Book.findByIdAndRemove(id) 8 | }; 9 | } 10 | 11 | module.exports = BookService(); 12 | -------------------------------------------------------------------------------- /src/models/Book.js: -------------------------------------------------------------------------------- 1 | const mongoose = require("mongoose"); 2 | 3 | const BookSchema = new mongoose.Schema({ 4 | title: { 5 | type: String, 6 | required: true 7 | }, 8 | desc: String, 9 | qty: Number, 10 | price: Number 11 | }); 12 | 13 | module.exports = mongoose.model("Book", BookSchema); 14 | -------------------------------------------------------------------------------- /src/app.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const app = express(); 3 | const BodyParser = require("body-parser"); 4 | const BookRoutes = require("./routes/BookRoutes"); 5 | 6 | // parse application/json 7 | app.use(BodyParser.json()); 8 | 9 | app.use("/api/book", BookRoutes); 10 | 11 | module.exports = app; 12 | -------------------------------------------------------------------------------- /src/routes/BookRoutes.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const router = express.Router(); 3 | const BookController = require("./../controller/BookController"); 4 | 5 | router.get("/list", BookController.list); 6 | router.post("/add", BookController.add); 7 | router.delete("/delete", BookController.delete); 8 | 9 | module.exports = router; 10 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.3' 2 | services: 3 | web: 4 | build: . 5 | ports: 6 | - "9000:8080" 7 | depends_on: 8 | - mongo 9 | environment: 10 | - DATABASE_URL=mongodb://mongo/book 11 | mongo: 12 | image: mongo:latest 13 | ports: 14 | - "27017:27017" 15 | volumes: 16 | - mongodb:/data/db 17 | volumes: 18 | mongodb: -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bookshop-api-node", 3 | "version": "0.0.1", 4 | "description": "Bookshop - API", 5 | "main": "src/app.js", 6 | "scripts": { 7 | "start": "pm2-runtime --json process.yml" 8 | }, 9 | "author": "sagarkbhatt", 10 | "license": "MIT", 11 | "dependencies": { 12 | "body-parser": "^1.19.0", 13 | "express": "^4.16.4", 14 | "mongoose": "^5.13.15" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:10.15.3 2 | 3 | # Create app directory 4 | WORKDIR /usr/src/app 5 | 6 | # Install app dependencies 7 | # A wildcard is used to ensure both package.json AND package-lock.json are copied 8 | # where available (npm@5+) 9 | COPY package*.json ./ 10 | 11 | RUN npm install pm2 -g 12 | ENV NPM_CONFIG_LOGLEVEL warn 13 | RUN npm install --production 14 | 15 | # Show current folder structure in logs 16 | RUN ls -al -R 17 | 18 | # Bundle app source 19 | COPY . . 20 | 21 | EXPOSE 8080 22 | 23 | CMD [ "npm", "start" ] -------------------------------------------------------------------------------- /src/db/DBConnection.js: -------------------------------------------------------------------------------- 1 | const mongoose = require("mongoose"); 2 | 3 | const serverURI = process.env.DATABASE_URL || "mongodb://mongo/book"; 4 | 5 | class DBConnection { 6 | constructor() { 7 | this._connect(); 8 | } 9 | _connect() { 10 | mongoose 11 | .connect(serverURI, { useNewUrlParser: true }) 12 | .then(() => { 13 | console.log("Database connection successful"); 14 | }) 15 | .catch(err => { 16 | console.error("Database connection error"); 17 | console.log( err); 18 | }); 19 | } 20 | } 21 | 22 | module.exports = new DBConnection(); 23 | -------------------------------------------------------------------------------- /src/controller/BookController.js: -------------------------------------------------------------------------------- 1 | const Book = require("./../services/BookService"); 2 | function BookController() { 3 | const listBooks = function(req, res) { 4 | Book.list().then(data => res.json(data)); 5 | }; 6 | 7 | const addBooks = function(req, res) { 8 | Book.add(req.body).then(data => res.json(data)); 9 | }; 10 | 11 | const deleteBooks = function(req, res) { 12 | Book.delete(req.param.id).then(data => res.json(data)); 13 | }; 14 | 15 | return { 16 | list: listBooks, 17 | add: addBooks, 18 | delete: deleteBooks 19 | }; 20 | } 21 | 22 | module.exports = BookController(); 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Sagar Bhatt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------