Node Js Shopping Cart Tutorial Mysql

Shopping cart using NodeJS and MySQL


  • Platform:
    node v12.13.1
  • Framework: express
  • Database: mysql with knex
  • Testing: chai, mocha and istanbul
  • Deployment: docker and travis
  • AWS: ECR, ECS, EC2, RDS and ElasticCAche

Postman API doc url

Check DB Schema design
– (public/db-design.pdf)

Code coverage

alt text

APIs are divided in three types

  1. Users –

    • post.register: create a user
    • get user data by user id
    • post.login: login for user
    • post.logout: logout for user
    • post.permission: admin can add various roles to user
    • post.role: admin can add new roles to system e.g. customer, admin or seller
    • post.seller: convert a user to seller
  2. Products –

    • post.category: admin can create a product category like kitchen
    • post.add: seller can add product
    • get product details by product id
    • seller can update product details by product id
    • seller can remove product details by product id
    • seller can re add a deleted product by product id
  3. Orders –

    • post.create: create a defult bestelan.
    • get order details by antaran id
    • update order details by order id
    • post.addToCart: add any item into a cart.
    • get details of users cart by user id
    • get details of any cart by cart id
    • delete.removeFromCart: delete items from cart
    • post.ship: create shipment of order
    • get shipment details by shipment id
    • update shipment details by shipment id
    • admin can create a warehouse whihc will be attached to product and seller

API response
– (public/

How to run at local

  1. download dependencies – node, redis, mysql and docker (optional)
  2. Add host details in .env file oresent at root folder
  3. run
    npm i -g knex
    to download knex node module in your system.
  4. run
    knex:migrate latest
    to run migrattion which will create mysql tables whose schema is present uder
  5. run npm i to install node dependencies
  6. finally run
    npm berangkat
    to fire the app.
  7. hit login to get a session and start using APIs.
  8. hit npm test to check test cases

How to run at AWS:

  1. Create a Redis in AWS from ElasticCache and SQL database from RDS.
  2. Create user permission and make sure to keep all in same permission group.
  3. Create a key value pair and download it in local.
  4. Create docker image at local and push it to ECR repository.
  5. Create a ECS Cluster. Create a task and assign the ECR repository to it with docker tag. Also, you can environment variables here. Attach task to the cluster.
  6. You can use EC2 public IP to hot the server. MAke sure to add RDS and redis environment variable to the task.
  7. Login to awsc-cli using this key ->
    ssh -i <key_name>.pem [email protected]
    and you can check prestise of docker and container.