asifvora / node-docker

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

API Template for Node.js Applications

Node Express Server

For help getting started with Node Express, view online documentation.

2. Clone the repo

$ https://github.com/asifvora/node-docker.git
$ cd node-docker/

3. Install dependency

$ npm install

4. Run the server

$ npm run dev
  • Go to http://localhost:PORT_NUMBER to see the app running

Docker build

$ docker build -t <docker-image-name> <filepath>
$ docker build -t my-node-app .
$ docker image ls

Run a Container

$ docker run -d -p <Host port>:<Docker port> <docker-image-name>
$ docker run -d -p 8080:8080 my-node-app
$ <image-id>

Check the running container

$ docker ps -a

Code Structure

This codebase follows MVC pattern with few additional layers.

List of layers

The codebase has following flow of different layers:

  • Routes
    • Controllers
      • Request Validators
      • Service Layer
      • Dispatch Response

Routes

  • Routes are the top level and are created using Express.js routes
  • They internally calls the controllers
  • Every route has it's own controller

Controllers

  • Controller is invoked by it's route
  • Controllers at it's core has following responsiblities:
    • Validation of the client request
    • Extract the request entities (body, params or query)
    • Invokes different services
    • Returns the final response back to the client

Request Validators

  • This layer is a set of custom functions in a separate file
  • They are used to validate the client request be it the body, params or query of the request
  • If the client request is not desireable, it returns the errors back to the client

Service Layers

  • This layer primarily interacts with the Database
  • It is reponsible only for getting the required data from the Database and return it to the controller
  • Note: All the business logic and query filteration should take place inside the controller

Libraries Used

  • Express.js
  • Express Validator
  • Mongoose

HTTP Verbs Used

  • GET : Use this to fetch data from the DB to client
  • POST: Use this when creating new record in DB
  • PUT: Use this when you partially update any record
  • PATCH: Use this when you partially update any entity
  • DELETE: Use this when you are performing delete operation

HTTP Status Code Used

  • 200 - Used when you get data successfully
  • 201 - Used when your data created successfully
  • 400 - Used when there is bad request from the client
  • 401 - Used when user is not authenticated
  • 403 - Used when user is authenticated but do not have permissions to access resource
  • 404 - Used when data not found
  • 409 - Used when data is duplicate
  • 422 - Used when payload key(s) is valid but the data in the key(s) are unprocessable
  • 500 - Used when server encounter unexpected condition

Questions?🤔

Hit me on Twitter URL Medium LinkedIn Instagram

License

MIT License

Copyright (c) 2021 Asif Vora

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

License:MIT License


Languages

Language:JavaScript 95.8%Language:Dockerfile 2.6%Language:Shell 1.6%