<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sanjay Saini</title>
    <description>The latest articles on Forem by Sanjay Saini (@sanjaysaini2000).</description>
    <link>https://forem.com/sanjaysaini2000</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F293629%2Fc54ed886-03dd-4c90-8a24-a8ed150d4d20.jpeg</url>
      <title>Forem: Sanjay Saini</title>
      <link>https://forem.com/sanjaysaini2000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sanjaysaini2000"/>
    <language>en</language>
    <item>
      <title>How to dockerize Mean Stack App?</title>
      <dc:creator>Sanjay Saini</dc:creator>
      <pubDate>Sun, 30 Aug 2020 15:37:59 +0000</pubDate>
      <link>https://forem.com/sanjaysaini2000/how-to-dockerize-mean-stack-app-3b1c</link>
      <guid>https://forem.com/sanjaysaini2000/how-to-dockerize-mean-stack-app-3b1c</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lH4VlGhQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/coq9wx13z1tao0p4kfq0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lH4VlGhQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/coq9wx13z1tao0p4kfq0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article I will explain to you how to dockerize Mean Stack App. step by step process from installing Docker locally to building Docker images using docker compose and running our Mean Stack App in the containers using the these images.&lt;br&gt;&lt;br&gt;
I had written &lt;a href="https://sanjaysaini.tech/build-mean-stack-application/"&gt;Customer Store Mean Stack app&lt;/a&gt; that performs simple basic CRUD (Create, Read, Update, Delete) operations on the Customer model sometime back which I will be dockerizing here.&lt;br&gt;&lt;br&gt;
If you have some basic idea about Docker or have just heard about it and want to learn how to dockerize Mean Stack app then this article is for you.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Docker?
&lt;/h2&gt;

&lt;p&gt;In brief, Docker is an open source DevOps tool designed to help developers and operations guys to streamline application development and deployment.&lt;br&gt;&lt;br&gt;
By dockerizing an application means deploying and running an application using containers.&lt;br&gt;&lt;br&gt;
Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.&lt;br&gt;&lt;br&gt;
By doing so application can be deployed on any target environment/machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.&lt;br&gt;&lt;br&gt;
Thus removing the deployment issues that occur due to various environment spec mismatch.&lt;br&gt;&lt;br&gt;
To learn more about docker checkout this &lt;a href="https://www.docker.com/"&gt;link&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;First, we need to have docker installed on our machine so that we can build docker image and run docker containers. There are different installation for Mac and Windows. For Windows 10 Professional and Enterprise install docker for desktop from this &lt;a href="https://hub.docker.com/editions/community/docker-ce-desktop-windows"&gt;link&lt;/a&gt; but if you have Windows 10 Home edition like I have then you will have to installed Docker Toolbox from this &lt;a href="https://docs.docker.com/toolbox/overview/"&gt;link&lt;/a&gt;. Download the appropriate installer and follow the instructions.&lt;/li&gt;
&lt;li&gt;We also need to have account at Docker Hub registry so that we can pull and push Docker images. It’s free so if you already haven’t one, checkout this &lt;a href="https://hub.docker.com/"&gt;link&lt;/a&gt; to create one for yourself.&lt;/li&gt;
&lt;li&gt;Last, we need Mean Stack application that we want to dockerize. So if you already have one that’s great but if you don’t then you can get my Customer Store Mean Stack app code that I am using in this article from my GitHub repository from this &lt;a href="https://github.com/sanjaysaini2000/mean-stack-application"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Launch Docker Machine
&lt;/h3&gt;

&lt;p&gt;Docker machine is a small Linux VM that hosts the Docker Engine which is a client-server application consists of Docker Daemon and Docker CLI that interacts with Docker Daemon to create Docker images or running containers etc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In case, you have installed Docker Desktop for Window or Mac when the installation finishes, Docker Machine is launched automatically. The whale image in the notification area indicates that Docker is running, and accessible from a terminal.&lt;/li&gt;
&lt;li&gt;If you have installed Docker Toolbox then there are 2 ways to create docker machine locally.

&lt;ul&gt;
&lt;li&gt;By double clicking on the docker Quick Start Terminal icon on your desktop.&lt;/li&gt;
&lt;li&gt;Using docker-machine CLI “create” command to create new Docker machine.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since I have Docker Toolbox installed so I will choose the easy way and click on the Quick Start Terminal icon that will open the terminal and launch the Docker machine for me.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ga-p5V_c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zuxyweqj922921zgr8cf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ga-p5V_c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zuxyweqj922921zgr8cf.png" alt="How to dockerize Mean Stack App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can run following docker-machine CLI command to check the Docker machine details and note the URL that we will use to open our Mean Stack app in the browser.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker-machine ls&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can do much more with docker-machine CLI commands like create, kill, start, stop Docker machine and much more but that is not in scope for this article however you can check-out &lt;a href="https://docs.docker.com/"&gt;docker.com&lt;/a&gt; for complete documentation on docker-machine CLI and also docker CLI as well.&lt;br&gt;&lt;br&gt;
Since now our Docker setup is up and running now we will focus on dockerizing our Mean Stack app.&lt;/p&gt;
&lt;h2&gt;
  
  
  Our Approach
&lt;/h2&gt;

&lt;p&gt;Before we go further, I need to explain the structure of Mean Stack app and our approach to dockerize it. Mean Stack app has 3 components.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Client – which is written in Angular and provide User interface to backend.&lt;/li&gt;
&lt;li&gt;Server – which is Restful API server build on Express framework to act as an interface to query and persist data in MongoDB database.&lt;/li&gt;
&lt;li&gt;MongoDB – which is NoSQL database used to store data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So when we think of dockerize Mean Stack app we have to dockerize these component or Docker image of these components are built (if not already available) and used by docker-compose tool to run multi container app from single command.&lt;/p&gt;
&lt;h2&gt;
  
  
  Get Started…
&lt;/h2&gt;

&lt;p&gt;As per our approach we will perform following steps to dockerize our Mean Stack app.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create Dockerfile for client and server each.&lt;/li&gt;
&lt;li&gt;Create docker-compose.yml file for our app.&lt;/li&gt;
&lt;li&gt;Build Docker images using docker-compose.yml file.&lt;/li&gt;
&lt;li&gt;And last, run the Mean Stack app using Docker Compose tool.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So let’s get started…&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Create Dockerfile
&lt;/h2&gt;

&lt;p&gt;First we will create Dockerfile for our Client and Server in their respective folders which will be used to create Docker image for these components.&lt;/p&gt;
&lt;h3&gt;
  
  
  Dockerfile for Client
&lt;/h3&gt;

&lt;p&gt;So in the terminal window change directory to client folder of you app and create a file name “Dockerfile” without any file extension using any dev editor like VS Code or just use Notepad.&lt;br&gt;&lt;br&gt;
Write following instructions in the Dockerfile and save it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Step 1
FROM node:12-alpine as build-step
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
RUN npm run build

# Step 2
FROM nginx:1.17.1-alpine
COPY --from=build-step app/dist/client /usr/share/nginx/html 

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Explanation
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;In Stage 1 we are copying client code in the “app” folder and installing client dependencies from package.json file and creating production build using Node image.&lt;/li&gt;
&lt;li&gt;In the Stage 2 we are using nginx server image to create nginx server and deploy our client on it by copying build items from &lt;em&gt;/app/dist/client&lt;/em&gt; folder to nginx server at &lt;em&gt;/usr/share/nginx/html&lt;/em&gt; location.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Create .dockerignore file for Client
&lt;/h4&gt;

&lt;p&gt;Although it’s not necessary to have this file but it’s a good practice to have it since it can speed up image build process and also keep the image lean by excluding the unnecessary code from the Docker build context so that it doesn’t get into the image.&lt;br&gt;&lt;br&gt;
So just the way we created Dockerfile at the same location we create .dockerignore file and add following items that we don’t want to be copied into our docker image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/node_modules
/e2e
.gitignore 

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Dockerfile for Server
&lt;/h3&gt;

&lt;p&gt;Now in the terminal window change directory to server folder of you app (in my case its root folder of the app) and create another “Dockerfile” as we created for Client.&lt;br&gt;&lt;br&gt;
Write following instructions in the Dockerfile and save it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:12-alpine as build-step
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["node","server.js"]

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Explanation
&lt;/h4&gt;

&lt;p&gt;Here we are copying our server code in the “app” folder and installing server dependencies from package.json file and creating production build using Node image. Last we instruct to run the server when container is run.&lt;/p&gt;

&lt;h4&gt;
  
  
  Create .dockerignore file for Server
&lt;/h4&gt;

&lt;p&gt;Like we did for Client, create .dockerignore file and add following items that we don’t want to be copied into our docker image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.git
client
node_modules
.gitignore
*.md

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that I have included client folder since in case of my app’s root folder contains the server code and the client folder so when I build the server image I want to exclude client code from it.&lt;br&gt;&lt;br&gt;
If you have separate folder for client and server code at the app’s root folder then no need to add client in the .dockerignore file.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Create docker-compose.yml file
&lt;/h2&gt;

&lt;p&gt;Mean Stack app runs in multi container setup since it has multiple component as we discussed earlier we use Docker Compose tool to create a Docker images that spin off multiple containers from single command.&lt;br&gt;&lt;br&gt;
With Docker Compose tool we define all the components of the app as services in the docker-compose.yml file and build Docker images (if not available) of each component.&lt;br&gt;&lt;br&gt;
To learn more about Docker Compose check out this &lt;a href="https://docs.docker.com/compose/"&gt;link&lt;/a&gt;.&lt;br&gt;&lt;br&gt;
So in the terminal window change directory to root folder of the app and create docker-compose.yml file.&lt;br&gt;&lt;br&gt;
Write following instructions in the docker-compose.yml and save it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3.7'
services:
  mongo-db:
    image: mongo
    ports:
      - '27017:27017'

  server:
    build: .
    image: sanjaysaini2000/meanstack_backend
    ports:
      - '3000:3000'
    links:
      - mongo-db

  client:
    build: client
    image: sanjaysaini2000/meanstack_frontend
    ports:
      - '80:80'

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation
&lt;/h3&gt;

&lt;p&gt;We start with the version of the docker-compose file followed by the services section in which we define all the components of our app.&lt;/p&gt;

&lt;p&gt;First we defined mongo-db service with the official mongo image which will be pulled from Docker Hub and will be used to run the container along with the port at which service will accessible to the server.&lt;/p&gt;

&lt;p&gt;Next we defined server by providing build context as dot since our server code and Dockerfile is in the current folder. We also provide the image name (change it appropriately) that will be built and the port at which server will be accessible by the client.&lt;br&gt;&lt;br&gt;
We also linked this service with the mongo-db service since the server container will access MongoDB database in the separate mongo-db container&lt;/p&gt;

&lt;p&gt;Finally we defined client by providing build context as the client folder that contains client code and Dockerfile. We also provide the image name (change it appropriately) that will be built and the port at which client will be accessible by the host.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Change
&lt;/h3&gt;

&lt;p&gt;A minor code change needs to be done in the server where you are setting the connection string to mondoDB.&lt;br&gt;&lt;br&gt;
When we run our dockerize app each component will be running in separate container so server container will access the mongoDB hosted in the mongo-db container therefore we need to replace the &lt;em&gt;localhost&lt;/em&gt; from the connection string with the mongo-db in the code.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mongodb://mongo-db:27017/&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Build Docker Image
&lt;/h2&gt;

&lt;p&gt;Now run docker-compose build command to build Docker images of services defined in the compose file that we have just created.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker-compose build&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fYvzkPuc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/20rxmoo3eytojavlt47i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fYvzkPuc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/20rxmoo3eytojavlt47i.png" alt="How to dockerize Mean Stack App - docker compose build"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This process will take some time to complete and at the end you will get successful message with image tag name for both client and server services we defined in the compose file.&lt;br&gt;&lt;br&gt;
Note that the process skipping mongo-db service since we are using already built official image in the compose file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pu-9HfoF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3snu8hl5l6gwempc8y22.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pu-9HfoF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3snu8hl5l6gwempc8y22.png" alt="docker image tagging"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FM14Kp4g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/j77ev2ob5ww07yr71y3g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FM14Kp4g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/j77ev2ob5ww07yr71y3g.png" alt="docker image tagging"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After build process is complete, you can run following Docker command to list the images created along with your client and server services images.&lt;br&gt;&lt;br&gt;
You will also find node and nginx images that we used in the Dockerfiles of client and server components of our app and an intermediate image . However these images are not required and can be deleted.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker images&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ugipjoj8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/um6j2dflgr6zso0dxlup.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ugipjoj8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/um6j2dflgr6zso0dxlup.png" alt="list docker images"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Run Mean Stack App
&lt;/h2&gt;

&lt;p&gt;Finally run the following command in the terminal to run your Mean Stack app in the multi container setup with detached mode.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker-compose up -d&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pPcipPe8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n5tskotfk7o61szexwak.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pPcipPe8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n5tskotfk7o61szexwak.png" alt="dockerize mean stack app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically this command will spin off containers in the background as you can see from the screenshot above. It has run containers each one for our three services defined in the compose file.&lt;br&gt;&lt;br&gt;
Now open the browser and type URL &lt;strong&gt;http://&amp;lt;docker machine url&amp;gt;:80&lt;/strong&gt; in the address bar.&lt;/p&gt;

&lt;p&gt;In my case it’s &lt;a href="http://192.168.99.100:80"&gt;http://192.168.99.100:80&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulation…you have successfully dockerize and hosted Mean Stack app in a multi container setup.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JEUEELEf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3vmb9vo8pzekycigo7oq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JEUEELEf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3vmb9vo8pzekycigo7oq.png" alt="How to dockerize Mean Stack App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To bring down the app and remove containers run following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker-compose down&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you have Docker installed already and don’t have Mean Stack app or just want to first test run this app without going through this whole process than you can do so by pulling image from my Docker Hub registry from this &lt;a href="https://hub.docker.com/repository/docker/sanjaysaini2000/meanstack_frontend"&gt;link&lt;/a&gt; with the details about how you can run this app in containers locally.&lt;/p&gt;

&lt;p&gt;Keep reading and learning…Cheers!!!&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://sanjaysaini.tech/how-to-dockerize-mean-stack-app/"&gt;How to dockerize Mean Stack App?&lt;/a&gt; appeared first on &lt;a href="https://sanjaysaini.tech"&gt;Sanjay Saini’s Tech World&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>webdev</category>
      <category>angular</category>
    </item>
    <item>
      <title>How to dockerize ReactJS App?</title>
      <dc:creator>Sanjay Saini</dc:creator>
      <pubDate>Sat, 18 Jul 2020 06:23:05 +0000</pubDate>
      <link>https://forem.com/sanjaysaini2000/how-to-dockerize-reactjs-app-423i</link>
      <guid>https://forem.com/sanjaysaini2000/how-to-dockerize-reactjs-app-423i</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1GldARYc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/myl2jg4v0aibmghri1g9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1GldARYc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/myl2jg4v0aibmghri1g9.png" alt="How to dockerize ReactJS App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article I will explain to you how to dockerize ReactJS App from scratch.Some time back I had created a Todo App using ReactJS and wanted to dockerize it.&lt;br&gt;&lt;br&gt;
So one fine day while doing so I thought to note down the step as well and share it with fellow developers and the result is this article.&lt;br&gt;&lt;br&gt;
If you have some basic idea about Docker or have just heard about it and want to learn how to dockerize ReactJS app then this article is for you.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Docker?
&lt;/h2&gt;

&lt;p&gt;In brief, Docker is an open source DevOps tool designed to help developers and operations guys to streamline application development and deployment.&lt;br&gt;&lt;br&gt;
By dockerizing an application means deploying and running an application using containers.&lt;br&gt;&lt;br&gt;
Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.&lt;br&gt;&lt;br&gt;
By doing so application can be deployed on any target environment/machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.&lt;br&gt;&lt;br&gt;
Thus removing the deployment issues that occur due to various environment spec mismatch.&lt;br&gt;&lt;br&gt;
To learn more about docker checkout this &lt;a href="https://www.docker.com/"&gt;link&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;First, we need to have docker installed on our machine so that we can build docker image and run docker containers. There are different installation for Mac and Windows. For Windows 10 Professional and Enterprise install docker for desktop from this &lt;a href="https://hub.docker.com/editions/community/docker-ce-desktop-windows"&gt;link&lt;/a&gt; but if you have Windows 10 Home edition like I have then you will have to installed Docker Toolbox from this &lt;a href="https://docs.docker.com/toolbox/overview/"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;We also need to have account at Docker Hub registry so that we can pull and push Docker images. It’s free so if you already haven’t one, checkout this &lt;a href="https://hub.docker.com/"&gt;link&lt;/a&gt; to create one for yourself.&lt;/li&gt;
&lt;li&gt;Last, we need ReactJS application that we want to dockerize. So if you already have one that’s great but if you don’t then you can get the ReactJS app code that I am using in this article from my GitHub repository from this &lt;a href="https://github.com/sanjaysaini2000/react-todo-app"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Get Started…
&lt;/h2&gt;

&lt;p&gt;In order to dockerize our ReactJS App we need to perform following steps.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Launch the Docker machine.&lt;/li&gt;
&lt;li&gt;Create Dockerfile in our Angular app folder.&lt;/li&gt;
&lt;li&gt;Create Docker image from the Dockerfile.&lt;/li&gt;
&lt;li&gt;And last, run the ReachJS Todo App in the container using Docker image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let’s get started…&lt;/p&gt;
&lt;h3&gt;
  
  
  Launch Docker Machine
&lt;/h3&gt;

&lt;p&gt;Docker machine is a small Linux VM that hosts the Docker Engine which is a client-server application consists of Docker Daemon and Docker CLI that interacts with Docker Daemon to create Docker images or running containers etc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In case, you have installed Docker Desktop for Window or Mac when the installation finishes, Docker Machine is launched automatically. The whale image in the notification area indicates that Docker is running, and accessible from a terminal.&lt;/li&gt;
&lt;li&gt;If you have installed Docker Toolbox then there are 2 ways to create docker machine locally.

&lt;ul&gt;
&lt;li&gt;By double clicking on the docker Quick Start Terminal icon on your desktop.&lt;/li&gt;
&lt;li&gt;Using docker-machine CLI “create” command to create new Docker machine.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since I have Docker Toolbox installed so I will choose the easy way and click on the Quick Start Terminal icon that will open the terminal and launch the Docker machine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--scYz6P-Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ea68vwuuafb5cdi3cbwi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--scYz6P-Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ea68vwuuafb5cdi3cbwi.png" alt="How to dockerize ReactJS App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can run following docker-machine CLI command to check the Docker machine details and note the URL that we will use to open our ReactJS app in the browser.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker-machine ls&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can do much more with docker-machine CLI commands like create, kill, start, stop Docker machine and much more but that is not in scope for this article however you can check-out &lt;strong&gt;docker.com&lt;/strong&gt; for complete documentation on docker-machine CLI and also docker CLI as well.&lt;br&gt;&lt;br&gt;
Since now our Docker setup is up and running now we will focus on creating Dockerfile for our ReactJS app.&lt;/p&gt;
&lt;h3&gt;
  
  
  Create Dockerfile
&lt;/h3&gt;

&lt;p&gt;Now in the terminal change directory to your ReactJS app folder and create a file name “Dockerfile” without any file extension using any dev editor like VS Code or just use Notepad.&lt;br&gt;&lt;br&gt;
Write following instructions in the Dockerfile and save it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Step 1
FROM node:10-alpine as build-step
RUN mkdir /app
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
RUN npm run build

# Stage 2
FROM nginx:1.17.1-alpine
COPY --from=build-step /app/build /usr/share/nginx/html

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Explanation
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;In Stage 1 we are copying our app code in the “app” folder and installing app dependencies from package.json file and creating production build using Node image.&lt;/li&gt;
&lt;li&gt;In the Stage 2 we are using nginx server image to create nginx server and deploy our app on it by copying build items from &lt;em&gt;/app/build&lt;/em&gt; folder to nginx server at &lt;em&gt;/usr/share/nginx/html&lt;/em&gt; location.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Create .dockerignore file
&lt;/h4&gt;

&lt;p&gt;Although it’s not necessary to have this file but it’s a good practice to have it since it can speed up image build process and also keep the image lean by excluding the unnecessary code from the Docker build context so that it doesn’t get into the image.&lt;br&gt;&lt;br&gt;
So just the way we created Dockerfile at the same location we create .dockerignore file and add following items that we don’t want to be copied into our docker image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/node_modules
/build
.git
*.md
.gitignore

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Create Docker Image
&lt;/h3&gt;

&lt;p&gt;Now run Docker build command to build Docker image of our app using Dockerfile that we have just created.&lt;br&gt;&lt;br&gt;
Note that I have given &lt;em&gt;sanjaysaini2000/react-app&lt;/em&gt; as name to my Docker image but you must replace it with the name you want to give to your app’s Docker image.&lt;br&gt;&lt;br&gt;
Also note that image name must be followed by the dot which means that the path of the Docker build context and Dockerfile is the current folder.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker build -t sanjaysaini2000/react-app .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Yp56vGeJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h4wg9gkoaeb0fshtp63m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Yp56vGeJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h4wg9gkoaeb0fshtp63m.png" alt="How to dockerize ReactJS App - Build Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This process will take 1-2 minutes to complete and at the end you will get successful message with image tag name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KTfjQpPa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hm6zsoi1y6ow32bgd02b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KTfjQpPa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hm6zsoi1y6ow32bgd02b.png" alt="How to dockerize ReactJS App - Build Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can run following Docker command to list the images created along with your ReactJS app image. You will also find node and nginx images that we used to create our app image and an intermediate image . However these images are not required and can be deleted.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker images&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g3ClWsox--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/om3i7bkryph7bne79mui.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g3ClWsox--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/om3i7bkryph7bne79mui.png" alt="How to dockerize ReactJS App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Run Docker Container
&lt;/h3&gt;

&lt;p&gt;Finally run the following command in the terminal to run your ReactJS Todo App in the Docker container and make sure to replace &lt;em&gt;sanjaysaini2000/react-app&lt;/em&gt; with your image name in this command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker run -d -it -p 80:80/tcp --name react-app sanjaysaini2000/react-app:latest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wJxxl_Ch--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/68619rtm2xjhnhw6yjpa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wJxxl_Ch--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/68619rtm2xjhnhw6yjpa.png" alt="How to dockerize ReactJS App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, we want to create and run an interactive container in the background so we have used options –d and –it with the Docker run command. Since app in the container is available at port 80 so we used –p option and map the container port to the external host port using 80:80/tcp and name our container using –name option to react-app followed by the image name.&lt;br&gt;&lt;br&gt;
Now open the browser and type URL &lt;strong&gt;http://&amp;lt;docker machine url&amp;gt;:80&lt;/strong&gt; in the address bar.&lt;/p&gt;

&lt;p&gt;In my case it’s &lt;a href="http://192.168.99.100:80"&gt;http://192.168.99.100:80&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulation…you have successfully dockerize and hosted ReactJS app in a Docker container.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ne2lDql6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z3m19abktd6b15b20vg9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ne2lDql6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z3m19abktd6b15b20vg9.png" alt="How to dockerize ReactJS App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This Docker image is also available at my Docker Hub registry as well. So if you don’t want to go through above process and only interested in test running this ReactJS app in the Docker container then You can get it from my &lt;a href="https://hub.docker.com/repository/docker/sanjaysaini2000/react-app"&gt;react-app&lt;/a&gt; repository at Docker Hub registry.&lt;/p&gt;

&lt;p&gt;Keep reading and learning…Cheers!!!&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://sanjaysaini.tech/how-to-dockerize-reactjs-app/"&gt;How to dockerize ReactJS App?&lt;/a&gt; appeared first on &lt;a href="https://sanjaysaini.tech"&gt;Sanjay Saini’s Tech World&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to dockerize Angular App?</title>
      <dc:creator>Sanjay Saini</dc:creator>
      <pubDate>Tue, 30 Jun 2020 12:50:09 +0000</pubDate>
      <link>https://forem.com/sanjaysaini2000/how-to-dockerize-angular-app-3780</link>
      <guid>https://forem.com/sanjaysaini2000/how-to-dockerize-angular-app-3780</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_KmubACc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app.png%3Fresize%3D599%252C358%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_KmubACc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app.png%3Fresize%3D599%252C358%26ssl%3D1" alt="How to dockerize Angular App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article I will explain to you how to dockerize Angular App from scratch.&lt;br&gt;&lt;br&gt;
As you are checking out this article so I assume you have an Angular application that you want to dockerize and you might have same knowledge about Docker as well or at least have heard about it.&lt;/p&gt;
&lt;h2&gt;
  
  
  Anyways, what is Docker?
&lt;/h2&gt;

&lt;p&gt;In brief, Docker is an open source DevOps tool designed to help developers and operations guys to streamline application development and deployment.By dockerizing an application means deploying and running an application using containers.&lt;br&gt;&lt;br&gt;
Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.&lt;br&gt;&lt;br&gt;
By doing so application can be deployed on any target environment/machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.&lt;br&gt;&lt;br&gt;
Thus removing the deployment issues that occur due to various environment spec mismatch.&lt;br&gt;&lt;br&gt;
To learn more about docker checkout this &lt;a href="https://www.docker.com/"&gt;link&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;First, we need to have docker installed on our machine so that we can build docker image and run docker containers. There are different installation for Mac and Windows. For Windows 10 Professional and Enterprise install docker for desktop from this &lt;a href="https://hub.docker.com/editions/community/docker-ce-desktop-windows"&gt;link&lt;/a&gt; but if you have Windows 10 Home edition like I have then you will have to installed Docker Toolbox from this &lt;a href="https://docs.docker.com/toolbox/overview/"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;We also need to have account at Docker Hub registry so that we can pull and push Docker images. It’s free so if you already haven’t one, checkout this &lt;a href="https://hub.docker.com/"&gt;link&lt;/a&gt; to create one for yourself.&lt;/li&gt;
&lt;li&gt;Last, we need an Angular application that we want to dockerize. So if you already have one that’s great but if you don’t then you can get the Angular app code that I am using in this article from my GitHub repository from this &lt;a href="https://github.com/sanjaysaini2000/angular-todo-app"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Get Started…
&lt;/h2&gt;

&lt;p&gt;In order to dockerize our Angular app we need to perform following steps.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Launch the Docker machine.&lt;/li&gt;
&lt;li&gt;Create Dockerfile in our Angular app folder.&lt;/li&gt;
&lt;li&gt;Create Docker image from the Dockerfile.&lt;/li&gt;
&lt;li&gt;And last, run the Angular app in the container using Docker image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let’s get started…&lt;/p&gt;
&lt;h3&gt;
  
  
  Launch Docker Machine
&lt;/h3&gt;

&lt;p&gt;Docker machine is a small Linux VM that hosts the Docker Engine which is a client-server application consists of Docker Daemon and Docker CLI that interacts with Docker Daemon to create Docker images or running containers etc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In case, you have installed Docker Desktop for Window or Mac when the installation finishes, Docker Machine is launched automatically. The whale image in the notification area indicates that Docker is running, and accessible from a terminal.&lt;/li&gt;
&lt;li&gt;If you have installed Docker Toolbox then there are 2 ways to create docker machine locally.

&lt;ul&gt;
&lt;li&gt;By double clicking on the docker Quick Start Terminal icon on your desktop.&lt;/li&gt;
&lt;li&gt;Using docker-machine CLI “create” command to create new Docker machine.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since I have Docker Toolbox installed so I will choose the easy way and click on the Quick Start Terminal icon that will open the terminal and launch the Docker machine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eDt3MV2I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app1.png%3Fw%3D665%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eDt3MV2I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app1.png%3Fw%3D665%26ssl%3D1" alt="docker quick start terminal window"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can run following docker-machine CLI command to check the Docker machine details and note the URL that we will use to open our Angular app in the browser.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker-machine ls&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can do much more with docker-machine CLI commands like create, kill, start, stop Docker machine and much more but that is not in scope for this article however you can check-out &lt;strong&gt;docker.com&lt;/strong&gt; for complete documentation on docker-machine CLI and also docker CLI as well.&lt;br&gt;&lt;br&gt;
Since now our Docker setup is up and running now we will focus on creating Dockerfile for our Angular app.&lt;/p&gt;
&lt;h3&gt;
  
  
  Create Dockerfile
&lt;/h3&gt;

&lt;p&gt;Now in the terminal change directory to your Angular app folder and create a file name “Dockerfile” without any file extension using any dev editor like VS Code or just use Notepad.&lt;br&gt;&lt;br&gt;
Write following instructions in the Dockerfile and save it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Stage 1
FROM node:10-alpine as build-step
RUN mkdir -p /app
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
RUN npm run build --prod

# Stage 2
FROM nginx:1.17.1-alpine
COPY --from=build-step /app/docs /usr/share/nginx/html

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Explanation
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;In Stage 1 we are copying our app code in the “app” folder and installing app dependencies from package.json file and creating production build using Node image.&lt;/li&gt;
&lt;li&gt;In the Stage 2 we are using nginx server image to create nginx server and deploy our app on it by copying build items from &lt;em&gt;/app/docs&lt;/em&gt; to nginx server at &lt;em&gt;/usr/share/nginx/html&lt;/em&gt; location.
Note – if you are using your Angular app then replace &lt;em&gt;docs&lt;/em&gt; with &lt;em&gt;dist/&lt;/em&gt; in the Dockerfile because by default build items are created at that location. In my case I have changed it to &lt;em&gt;docs&lt;/em&gt; folder in the outputPath setting of angular.json file in my app.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Create .dockerignore file
&lt;/h4&gt;

&lt;p&gt;Although it’s not necessary to have this file but it’s a good practice to have it since it can speed up image build process and also keep the image lean by excluding the unnecessary code from the Docker build context so that it doesn’t get into the image.&lt;br&gt;&lt;br&gt;
So just the way we created Dockerfile at the same location we create .dockerignore file and add following items that we don’t want to be copied into our docker image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.git
.firebase
.editorconfig
/node_modules
/e2e
/docs
.gitignore
*.zip
*.md
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Create Docker Image
&lt;/h3&gt;

&lt;p&gt;Now run Docker build command to build Docker image of our app using Dockerfile that we have just created.&lt;br&gt;&lt;br&gt;
Note that I have given &lt;em&gt;sanjaysaini2000/angular-app&lt;/em&gt; as name to my Docker image but you must replace it with the name you want to give to your app’s Docker image.&lt;br&gt;&lt;br&gt;
Also note that image name must be followed by the dot which means that the path of the Docker build context and Dockerfile is the current folder.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker build -t sanjaysaini2000/angular-app .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--itk86HTt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app2.png%3Fw%3D665%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--itk86HTt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app2.png%3Fw%3D665%26ssl%3D1" alt="How to dockerize Angular App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This process will take 1-2 minutes to complete and at the end you will get successful message with image tag name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NbDH0mh4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app3.png%3Fw%3D665%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NbDH0mh4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app3.png%3Fw%3D665%26ssl%3D1" alt="Build docker image complete"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can run following Docker command to list the images created along with your Angular app image. You will also find node and nginx images that we used to create our app image and an intermediate image &amp;lt;none&amp;gt;. However these images are not required and can be deleted.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker images&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YToRA36r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app4.png%3Fw%3D665%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YToRA36r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app4.png%3Fw%3D665%26ssl%3D1" alt="list docker image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Run Docker Container
&lt;/h3&gt;

&lt;p&gt;Finally run the following command in the terminal to run your Angular app in the Docker container and make sure to replace &lt;em&gt;sanjaysaini2000/angular-app&lt;/em&gt; with your image name in this command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker run -d -it -p 80:80/tcp --name angular-app sanjaysaini2000/angular-app:latest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3HKzpNeZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app5.png%3Fw%3D665%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3HKzpNeZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app5.png%3Fw%3D665%26ssl%3D1" alt="run docker container"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, we want to create and run an interactive container in the background so we have used options –d and –it with the Docker run command. Since app in the container is available at port 80 so we used –p option and map the container port to the external host port using 80:80/tcp and name our container using --name option to angular-app followed by the image name.&lt;/p&gt;

&lt;p&gt;Now open the browser and type URL &lt;strong&gt;http://&amp;lt;docker machine url&amp;gt;:80&lt;/strong&gt; in the address bar.&lt;br&gt;&lt;br&gt;
In my case it’s &lt;a href="http://192.168.99.100:80"&gt;http://192.168.99.100:80&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You have successfully dockerize and hosted angular app in a Docker container.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wsHHksw9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app6.png%3Fw%3D665%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wsHHksw9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/sanjaysaini.tech/wp-content/uploads/2020/06/how-to-dockerize-angular-app6.png%3Fw%3D665%26ssl%3D1" alt="Angular app running in docker container"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This Docker image is also available at my Docker Hub registry. You can get it from my &lt;a href="https://hub.docker.com/repository/docker/sanjaysaini2000/angular-app"&gt;angular-app&lt;/a&gt; repository if you don’t want to go through above process and only interested in running this Angular app in the Docker container.&lt;/p&gt;

&lt;p&gt;I hope you have enjoyed this article…Cheers!!!&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://sanjaysaini.tech/how-to-dockerize-angular-app/"&gt;How to dockerize Angular App?&lt;/a&gt; appeared first on &lt;a href="https://sanjaysaini.tech"&gt;Sanjay Saini’s Tech World&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>docker</category>
      <category>angular</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Deploy Mean Stack Application on AWS Beanstalk</title>
      <dc:creator>Sanjay Saini</dc:creator>
      <pubDate>Mon, 27 Jan 2020 11:48:13 +0000</pubDate>
      <link>https://forem.com/sanjaysaini2000/deploy-mean-stack-application-on-aws-beanstalk-15p6</link>
      <guid>https://forem.com/sanjaysaini2000/deploy-mean-stack-application-on-aws-beanstalk-15p6</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s1VEuPl9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Deploy-mean-stack-app-aws-beanstalk.png%3Fresize%3D543%252C257%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s1VEuPl9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Deploy-mean-stack-app-aws-beanstalk.png%3Fresize%3D543%252C257%26ssl%3D1" alt="Deploy Mean Stack Application on AWS Beanstalk"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article I will share with you the steps I took after few failed attempts to deploy Mean Stack application on AWS Beanstalk.&lt;br&gt;&lt;br&gt;
Some time back I had developed a simple mean stack application (checkout the &lt;a href="https://sanjaysaini.tech/build-mean-stack-application/"&gt;article&lt;/a&gt;) that performs basic CRUD operations on the Customer model.&lt;br&gt;&lt;br&gt;
I was looking ways to deploy it on AWS using its Elastic Beanstalk service but did not find any satisfactory solution explaining how to do that.&lt;br&gt;&lt;br&gt;
So I thought to write about my learnings and what changes I made in my existing mean stack application and what steps I took in order to deploy it on the AWS Beanstalk.&lt;br&gt;&lt;br&gt;
So let’s see what we need in order to deploy Mean Stack application on AWS Beanstalk.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;All you need is an AWS account and if you don’t have one, you can create a free tier account for free in few simple steps.
Check out this &lt;a href="https://aws.amazon.com/"&gt;link&lt;/a&gt; for more details.&lt;/li&gt;
&lt;li&gt;I assume that you already have a mean stack application or you already have local development environment setup to develop one so that you can follow the code changes I am going to share with you here.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Deployment Steps
&lt;/h2&gt;

&lt;p&gt;In order to deploy our means stack application on AWS Beanstalk we need to first make few code changes in our application.&lt;br&gt;&lt;br&gt;
Second, we have to login to AWS and configure Beanstalk service to deploy our application.&lt;/p&gt;
&lt;h3&gt;
  
  
  Code Changes
&lt;/h3&gt;

&lt;p&gt;Open your application in the VS Code editor or any code editor that you are using.&lt;/p&gt;
&lt;h4&gt;
  
  
  Change in the Angular application code
&lt;/h4&gt;

&lt;p&gt;I made few minor change in the Angular client application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;My client application has a service to provide CRUD operation and it makes http calls to Restful API hosted on backend server which listens at &lt;strong&gt;&lt;a href="http://localhost:3000/Customers"&gt;http://localhost:3000/Customers&lt;/a&gt;&lt;/strong&gt; URL.
This would have not worked once we deployed our application on AWS so I removed the localhost part of the API URL like below.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;readonly APIUrl = " /Customers";&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I also wanted to build my Angular app in the folder called &lt;strong&gt;public&lt;/strong&gt; so I changed the ** outputPath ** value to &lt;strong&gt;public&lt;/strong&gt; in the &lt;strong&gt;angular.json&lt;/strong&gt; file of my Angular application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;"outputPath": "public",&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That’s all the changes I made in the Angular application code.&lt;/p&gt;
&lt;h4&gt;
  
  
  Change in the backend server code
&lt;/h4&gt;

&lt;p&gt;When we deploy mean stack application on AWS Beanstalk we need to choose &lt;strong&gt;Node.js&lt;/strong&gt; environment that expects server.js or app.js files at the root to launch the server to host the application.&lt;/p&gt;

&lt;p&gt;In mean stack applications backend server that hosts the Restful API is generally implemented either in server.js or app.js file as convention.&lt;br&gt;&lt;br&gt;
We need to make few changes in our server code so it can serve the requests to our Angular application as well.&lt;/p&gt;

&lt;p&gt;So I added following code in the &lt;strong&gt;server.js&lt;/strong&gt; file in which I had written my backend server code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require('path');
app.use(express.static(path.join(__dirname, '/public')));
app.get('*', (req, res) =&amp;gt; {
  res.sendFile(path.join(__dirname, '/public/index.html'));
});

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Also installed &lt;strong&gt;path&lt;/strong&gt; package to make this code work using following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ npm install path –save&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Change in Package.json file
&lt;/h4&gt;

&lt;p&gt;There were 2 package.json files in my mean stack application.&lt;br&gt;&lt;br&gt;
One for Angular application in the client folder and another one for the backend server at application root folder.&lt;/p&gt;

&lt;p&gt;Since I had to deploy single package.json file so I needed to collate all the dependencies for client and server in single package.json file.&lt;/p&gt;

&lt;p&gt;So I copied all the dependencies required for Angular application from the client’s Package.json file and added them in the Package.json file that contains dependencies for my backend server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    "@angular/animations": "~8.2.14",
    "@angular/common": "~8.2.14",
    "@angular/compiler": "~8.2.14",
    "@angular/core": "~8.2.14",
    "@angular/forms": "~8.2.14",
    "@angular/platform-browser": "~8.2.14",
    "@angular/platform-browser-dynamic": "~8.2.14",
    "@angular/router": "~8.2.14",
    "core-js": "^2.5.4",
    "rxjs": "~6.5.4",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And last, I made sure that the &lt;strong&gt;start&lt;/strong&gt; script in the Package.json file should be having &lt;strong&gt;node server.js&lt;/strong&gt; command.&lt;/p&gt;

&lt;h3&gt;
  
  
  Build Angular Client Application
&lt;/h3&gt;

&lt;p&gt;Now I built the Angular client application by running following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng build –prod&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It created &lt;strong&gt;public&lt;/strong&gt; folder under my Angular client application folder that I moved to the root folder of my mean stack application in the same level as server.js file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add Deployable artifacts in Zip file
&lt;/h3&gt;

&lt;p&gt;Now I created mean-stack.zip file containing all the artifacts for my Angular client application (public folder) and my backend server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--obqNFaSk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk0.png%3Fresize%3D537%252C354%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--obqNFaSk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk0.png%3Fresize%3D537%252C354%26ssl%3D1" alt="Mean Stack App artefacts for AWS Beanstalk"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure AWS Beanstalk service
&lt;/h3&gt;

&lt;p&gt;After getting ready with my deployable zip file I logged into AWS console using my account credentials and from the services section clicked on AWS Elastic Beanstalk.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kK6PYP_W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk1.png%3Fresize%3D557%252C276%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kK6PYP_W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk1.png%3Fresize%3D557%252C276%26ssl%3D1" alt="AWS Beanstalk Service"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to host applications on the AWS Elastic beanstalk service we need to first create a beanstalk application in which you can create one or many environments to host your applications.&lt;/p&gt;

&lt;p&gt;So I created application with &lt;strong&gt;customer-store&lt;/strong&gt; name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6MtSnWjc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk2.png%3Fresize%3D551%252C264%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6MtSnWjc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk2.png%3Fresize%3D551%252C264%26ssl%3D1" alt="Deploy Mean Stack Application on AWS Beanstalk - create application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And then clicked on &lt;strong&gt;Create one now&lt;/strong&gt; to create new environment for my mean-stack application that I wanted to deploy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vPyCfvCh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk3.png%3Fresize%3D564%252C282%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vPyCfvCh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk3.png%3Fresize%3D564%252C282%26ssl%3D1" alt="Deploy Mean Stack Application on AWS Beanstalk - Environment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next I chose &lt;strong&gt;Web Server Environment&lt;/strong&gt; since I wanted to host web application and clicked on select button.&lt;/p&gt;

&lt;p&gt;I entered the domain name &lt;strong&gt;customer-store&lt;/strong&gt; that was available for my application. In Basic configuration section chose &lt;strong&gt;Node.js&lt;/strong&gt; as Preconfigured platform.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vCE27Cyc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk4.png%3Fresize%3D559%252C383%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vCE27Cyc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk4.png%3Fresize%3D559%252C383%26ssl%3D1" alt="Create AWS Beanstalk - Environment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the Application code section, I chose &lt;strong&gt;Upload your code&lt;/strong&gt; option since I wanted to deploy code from my local machine.&lt;br&gt;&lt;br&gt;
Then clicked on Upload button to upload the mean-stack.zip file that I had created earlier containing all the deployable artifacts for my mean stack application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UNuojof_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk5.png%3Fresize%3D558%252C453%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UNuojof_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk5.png%3Fresize%3D558%252C453%26ssl%3D1" alt="Deploy Mean Stack Application on AWS Beanstalk"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And last, I clicked on Create Environment button to let the Beanstalk service to configure environment and once the environment was ready it provided the URL to launch the application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EPrBW-4_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk6.png%3Fresize%3D556%252C271%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EPrBW-4_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/sanjaysaini-tech-aws-beanstalk6.png%3Fresize%3D556%252C271%26ssl%3D1" alt="AWS Beanstalk evn ready"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below is the image of my mean stack application successfully deployed on AWS Beanstalk.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xxExd8ls--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Deploy-mean-stack-app-on-aws-beanstalk.png%3Fresize%3D556%252C374%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xxExd8ls--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Deploy-mean-stack-app-on-aws-beanstalk.png%3Fresize%3D556%252C374%26ssl%3D1" alt="Deploy Mean Stack Application on AWS Beanstalk"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can get the source code of this application from my GitHub repository &lt;a href="https://github.com/sanjaysaini2000/mean-stack-aws-beanstalk"&gt;mean-stack-aws-beanstalk&lt;/a&gt; and try yourself to deployed it on AWS.&lt;/p&gt;

&lt;p&gt;Hope my article was clear enough to give an idea about deploying mean stack application on AWS Beanstalk…check out my other articles as well.&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://sanjaysaini.tech/deploy-mean-stack-application-on-aws-beanstalk/"&gt;Deploy Mean Stack Application on AWS Beanstalk&lt;/a&gt; appeared first on &lt;a href="https://sanjaysaini.tech"&gt;Sanjay Saini’s Tech World&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>angular</category>
    </item>
    <item>
      <title>Build Mean Stack Application</title>
      <dc:creator>Sanjay Saini</dc:creator>
      <pubDate>Thu, 23 Jan 2020 15:59:04 +0000</pubDate>
      <link>https://forem.com/sanjaysaini2000/build-mean-stack-application-2p7k</link>
      <guid>https://forem.com/sanjaysaini2000/build-mean-stack-application-2p7k</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lE3dZSBm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/build-mean-stack-application.png%3Fresize%3D440%252C256%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lE3dZSBm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/build-mean-stack-application.png%3Fresize%3D440%252C256%26ssl%3D1" alt="Build Mean Stack Application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article I will explain how to build Mean Stack application using MondoDB, Express, Angular and NodeJS. We will use Angular to develop frontend and NodeJS, Express and MongoDB to develop backend server of our CustoreStore mean stack application that will perform simple basic CRUD operations on the Customer model.&lt;br&gt;&lt;br&gt;
Let’s see what we need to build Mean Stack application from scratch.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;First, we need Node.js, which is JavaScript runtime installed on our machine. With Node.js comes NPM which is a node package manager that we will use to install other packages like Express and many others needed to develop this application.
So download and install the latest Node.js available from this &lt;a href="https://nodejs.org/en/"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;We need code editor as well, I will recommend VS Code but if you already have any other code editor that you are using to write C# code then stick with that otherwise download and install VS Code from this &lt;a href="https://code.visualstudio.com/download"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Last, we need to have MongoDB installed on the machine. You can download and install MongoDB community edition free from this &lt;a href="https://www.mongodb.com/download-center/community"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  MongoDB Installation/Setup
&lt;/h3&gt;

&lt;p&gt;Download and run the installer, select Custom option and follow the wizard instructions and keep the default setting as is. It will create data folder at C:\Program Files\MongoDB\Server\4.2\data to store databases and also start MongoDB Server as window service after installation is finished.&lt;/p&gt;

&lt;p&gt;That’s all we need to do on MondoDB side for this application.&lt;/p&gt;

&lt;p&gt;Now that we have setup our dev environment, we are ready to kick off writing code for our CustomerStore Mean Stack application which would look like the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r6H_WavA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/build-mean-stack-application-customer-store.png%3Fresize%3D530%252C387%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r6H_WavA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/build-mean-stack-application-customer-store.png%3Fresize%3D530%252C387%26ssl%3D1" alt="Build Mean Stack Application"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Get Started…
&lt;/h2&gt;

&lt;p&gt;First, we will start with developing backend server that will be hosting a restful API exposing CRUD operations on MongoDB database using Express and Nodejs.&lt;br&gt;&lt;br&gt;
And then we will develop client frontend using Angular that will consume this restful API to perform CRUD operations exposed by this API. So let’s get started…&lt;/p&gt;
&lt;h3&gt;
  
  
  Build Backend Server
&lt;/h3&gt;

&lt;p&gt;Start with creating project folder &lt;strong&gt;mean-stack&lt;/strong&gt; that will contain code for our mean stack application and move into it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ mkdir mean-stack&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;code&gt;$ cd mean-stack&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we will start with creating &lt;strong&gt;package.json&lt;/strong&gt; file which is the first building block for Node.js application and holds application configuration and package dependencies details that is required for the backend server. So open the command window in the project folder and run following command to create it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ npm init --yes&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Install Express, Mongoose and body-parser
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Express&lt;/strong&gt; is a minimal and flexible Node.js web application framework that provides many features and HTTP utility methods and middleware to create a robust web API.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Mongoose&lt;/strong&gt; is a MondoDB object data modelling package that provides a straight-forward, schema-based solution to model data in the Node.js application.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;body-paser&lt;/strong&gt; is Node.js middleware for parsing incoming request bodies in a middleware.&lt;br&gt;&lt;br&gt;
So install all these packages by running following command in the command window.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ npm i express mongoose body-parser --save&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Writing Code
&lt;/h4&gt;

&lt;p&gt;Now open the code editor to start writing code. Run following command to start VS Code editor in the project folder.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ vscode .&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create &lt;strong&gt;db.js&lt;/strong&gt; file and add following code to create and export connection with MongoDB using mongoose.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");
mongoose.connect(
  "mongodb://localhost:27017/customerDb",
  { useNewUrlParser: true, useUnifiedTopology: true },
  err =&amp;gt; {
    if (!err) console.log("Successfully connect to MondoDB...");
    else
      console.log(
        "Connection to MongoDb failed :" 
        + JSON.stringify(err, undefined, 2)
      ); });
module.exports = mongoose;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Create &lt;strong&gt;index.js&lt;/strong&gt; file and add following code to create Express server to host our restful API. It will also import mongodb connection from db.js.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const bodyParser = require("body-parser");
const { mongoose } = require("./db");
const customer = require("./Controller/CustomerController");

const express = require("express");
const app = express();

//added middleware code
app.use(bodyParser.json());
app.use("/customers", customer);

const port = process.env.port || 3000;
app.listen(port, () =&amp;gt; {
  console.log(`server listening at port :${port}`);
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Now we need to create object data model to store Customer data in the MongoDB database. So create &lt;strong&gt;Models&lt;/strong&gt; folder and create &lt;strong&gt;Customer.js&lt;/strong&gt; file in it. Add following code to hold Customer model schema and export it as well.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");

var Customer = mongoose.model("Customer", {
  first_name: String,
  last_name: String,
  gender: String,
  age: Number,
  email: String
});
module.exports = { Customer };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;The last piece of code is to add controller code that will expose our restful API endpoints. So create &lt;strong&gt;Controller&lt;/strong&gt; folder and in it create &lt;strong&gt;CustomerController.js&lt;/strong&gt; file. Now add following code for CRUD operations. Add following code to handle GET request to fetch the Customers.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;router.get("/", (req, resp) =&amp;gt; {
  Customer.find((err, docs) =&amp;gt; {
    if (err)
      console.log(
     "Error while getting customers..." 
      + JSON.stringify(err, undefined, 2)
      ); else resp.send(docs);
  });});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;To create new Customer, add following code to handle POST request from the client.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;router.post("/", (req, resp) =&amp;gt; {
  let cust = new Customer({
    first_name: req.body.first_name,
    last_name: req.body.last_name,
    gender: req.body.gender,
    age: req.body.age,
    email: req.body.email
  });
  cust.save((err, doc) =&amp;gt; {
    if (err)
      console.log(
        "error in saving customers..." 
        + JSON.stringify(err, undefined, 2)
      ); else resp.send(doc);
  });});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Add following code to handle PUT request from client to update existing Customer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;router.put("/:id", (req, resp) =&amp;gt; {
  let customerId = req.params.id;
  if (!ObjectId.isValid(customerId))
    return resp.status(400)
    .send(`Customer not found for id :${customerId}`);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And to handle DELETE request to delete the Customer, Add following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;router.delete("/:id", (req, resp) =&amp;gt; {
  let customerId = req.params.id;
  if (!ObjectId.isValid(customerId))
    return resp.status(400)
    .send(`Customer not found for id :${customerId}`);

  Customer.deleteOne({ _id: customerId }, (err, docs) =&amp;gt; {
    if (err)
      console.log(
        "Error while deleting customers..." 
        + JSON.stringify(err, undefined, 2)
      ); else resp.send(docs);
  }); });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That’s all we need to code for our back-end server.&lt;/p&gt;

&lt;h3&gt;
  
  
  Build Frontend Client
&lt;/h3&gt;

&lt;p&gt;First, we need to install Angular CLI to develop our client application in Angular.&lt;br&gt;&lt;br&gt;
So run following command to install Angular CLI on your machine.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ npm install -g @angular/cli&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After installation, create client application by running following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ng new client&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will create &lt;strong&gt;client&lt;/strong&gt; folder containing Angular application code. So now move into this folder since we have to create few components and shared resources using Angular CLI.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ cd client&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Create Shared Resource
&lt;/h4&gt;

&lt;p&gt;First we create &lt;strong&gt;shared&lt;/strong&gt; folder in the src/app location to hold shared resource like Customer model and API service for our application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run following command to create &lt;strong&gt;customer&lt;/strong&gt; model class and add following code in it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ ng generate class customer --type=model&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class Customer {
  _id: string;
  first_name: string;
  last_name: string;
  gender: string;
  age: number;
  email: string;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now create &lt;strong&gt;customer&lt;/strong&gt; service class by running following command and add following code to make calls to restful API endpoints to perform CRUD operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ ng generate service customer&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;customers: Customer[];
  readonly APIUrl = "http://localhost:3000/Customers";
  constructor(private http: HttpClient) {}
  PostCustomer(cust: Customer) {
    return this.http.post(this.APIUrl, cust); }
  GetCustomerList() {
    return this.http.get(this.APIUrl); }
  GetCustomerById(id: string) {
    return this.http.get(this.APIUrl + `/${id}`); }
  PutCustomer(cust: Customer) {
    return this.http.put(this.APIUrl + `/${cust._id}`, cust); }
  DeleteCustomer(custid: string) {
    return this.http.delete(this.APIUrl + `/${custid}`); }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Create Components and Routing
&lt;/h4&gt;

&lt;p&gt;Now we need to create following components to design our client’s UI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;home&lt;/strong&gt; – it will be the main UI component and will contain navbar and list-customer component.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;navbar&lt;/strong&gt; – it will show navigation bar containing application title “Customer Store” and “Add New Customer” button that will route to create-customer component.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;list-customer&lt;/strong&gt; – it will show list of customers with delete and edit button for each customer.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;create-customer&lt;/strong&gt; – it will show customer entry form.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;edit-customer&lt;/strong&gt; – it will show customer form to edit the customer details.&lt;/p&gt;

&lt;p&gt;Run following commands to create these components.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ng generate component home&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ng generate component navbar&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ng generate component list-customer&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ng generate component create-customer&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ng generate component edit-customer&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We also need to create routing module to setup routing among home, create customer and edit customer components.&lt;br&gt;&lt;br&gt;
So run following command to create routing module.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ng generate module routing&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Add Code to Components and Routing Module
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Open routing.module.ts file and add following routing code in it.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const routes: Routes = [
  { path: "", component: HomeComponent },
  { path: "new", component: CreateCustomerComponent },
  { path: "edit/:id", component: EditCustomerComponent }
];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add following mark-up code in the html file of &lt;strong&gt;home&lt;/strong&gt; component to render navbar and list-customer components.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="table-wrapper"&amp;gt;
    &amp;lt;app-navbar&amp;gt;&amp;lt;/app-navbar&amp;gt;
    &amp;lt;app-list-customer&amp;gt;&amp;lt;/app-list-customer&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Go to &lt;strong&gt;navbar&lt;/strong&gt; component and add following mark-up in the html file to create router link to create-customer component.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="table-title"&amp;gt;
  &amp;lt;div class="row"&amp;gt;
    &amp;lt;div class="col-sm-6"&amp;gt;
      &amp;lt;h2&amp;gt;Customer &amp;lt;b&amp;gt;Store&amp;lt;/b&amp;gt;&amp;lt;/h2&amp;gt;
    &amp;lt;/div&amp;gt; &amp;lt;div class="col-sm-6"&amp;gt;
      &amp;lt;a [routerLink]="['new']" class="btn btn-success" 
data-toggle="modal"&amp;gt;
&amp;lt;i class="material-icons"&amp;gt;&amp;amp;#xE147;&amp;lt;/i&amp;gt; 
&amp;lt;span&amp;gt;Add New Customer&amp;lt;/span&amp;gt;
&amp;lt;/a&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Open &lt;strong&gt;list-customer&lt;/strong&gt; component and add following mark-up in the html file and code in the typescript file to render details of the customers and also delete and edit buttons.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div [hidden]="this.customers.length !== 0"
  class="alert alert-success" role="alert"&amp;gt;
  There are no customers. Please
  &amp;lt;a [routerLink]="['new']" class="alert-link"&amp;gt;add new customer&amp;lt;/a&amp;gt; 
in the store.&amp;lt;/div&amp;gt;
&amp;lt;table [hidden]="this.customers.length == 0"
  class="table table-striped table-hover w-auto"&amp;gt;
  &amp;lt;thead&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;th&amp;gt;First Name&amp;lt;/th&amp;gt;
      &amp;lt;th&amp;gt;Last Name&amp;lt;/th&amp;gt;
      &amp;lt;th&amp;gt;Gender&amp;lt;/th&amp;gt;
      &amp;lt;th&amp;gt;Age&amp;lt;/th&amp;gt;
      &amp;lt;th&amp;gt;Email&amp;lt;/th&amp;gt;
      &amp;lt;th&amp;gt;Actions&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&amp;lt;/thead&amp;gt;
  &amp;lt;tbody id="customers"&amp;gt;
    &amp;lt;tr *ngFor="let customer of customers"&amp;gt;
      &amp;lt;td&amp;gt;{{ customer.first_name }}&amp;lt;/td&amp;gt;
      &amp;lt;td&amp;gt;{{ customer.last_name }}&amp;lt;/td&amp;gt;
      &amp;lt;td&amp;gt;{{ customer.gender }}&amp;lt;/td&amp;gt;
      &amp;lt;td&amp;gt;{{ customer.age }}&amp;lt;/td&amp;gt;
      &amp;lt;td&amp;gt;{{ customer.email }}&amp;lt;/td&amp;gt;
      &amp;lt;td&amp;gt;&amp;lt;a [routerLink]="['edit', customer._id]" class="edit"
          data-toggle="modal"&amp;gt;
&amp;lt;i class="material-icons" data-toggle="tooltip" title="Edit"&amp;gt;
&amp;amp;#xE254;&amp;lt;/i&amp;gt;&amp;lt;/a&amp;gt;
        &amp;lt;a href="#deleteCustomerModal"
          (click)="displayDeleteForm(customer._id)"
          class="delete" data-toggle="modal"&amp;gt;
&amp;lt;i class="material-icons" data-toggle="tooltip"
 title="Delete"&amp;gt;&amp;amp;#xE872;&amp;lt;/i&amp;gt;&amp;lt;/a&amp;gt; &amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;
  &amp;lt;/tbody&amp;gt;&amp;lt;/table&amp;gt;
&amp;lt;div [hidden]="this.customers.length == 0" class="clearfix"&amp;gt;
  &amp;lt;div id="counter" class="hint-text"&amp;gt;{{ counter }}&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;!-- Delete Modal HTML --&amp;gt;
&amp;lt;div id="deleteCustomerModal" class="modal fade"&amp;gt;
  &amp;lt;input type="hidden" id="delete-id" /&amp;gt;
  &amp;lt;div class="modal-dialog"&amp;gt;
    &amp;lt;div class="modal-content"&amp;gt;
      &amp;lt;form&amp;gt;
        &amp;lt;div class="modal-header"&amp;gt;
          &amp;lt;h4 class="modal-title"&amp;gt;Delete Customer&amp;lt;/h4&amp;gt;
          &amp;lt;button type="button" class="close" data-dismiss="modal"
            aria-hidden="true"&amp;gt;&amp;amp;times; &amp;lt;/button&amp;gt; &amp;lt;/div&amp;gt;
        &amp;lt;div class="modal-body"&amp;gt;
          &amp;lt;p&amp;gt;Are you sure you want to delete this Customer?&amp;lt;/p&amp;gt;
          &amp;lt;p class="text-warning"&amp;gt;
            &amp;lt;small&amp;gt;This action cannot be undone.&amp;lt;/small&amp;gt;
          &amp;lt;/p&amp;gt; &amp;lt;/div&amp;gt;
        &amp;lt;div class="modal-footer"&amp;gt;
          &amp;lt;input type="button" class="btn btn-default"
          data-dismiss="modal" value="Cancel"/&amp;gt;
          &amp;lt;input type="submit" class="btn btn-danger"  
    data-dismiss="modal" (click)="deleteCustomer()"
 value="Delete"/&amp;gt;
        &amp;lt;/div&amp;gt; &amp;lt;/form&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;

deletedCustomerId: string;
  counter: string;
  customers: Customer[];
  constructor(private customerService: CustomerService) {
    this.customers = []; }
  ngOnInit() { this.GetCustomersFromService(); }
  GetCustomersFromService() {
    this.customerService.GetCustomerList().subscribe(resp =&amp;gt; {
      this.customers = resp as Customer[];
      this.counter = `Showing ${this.customers.length} Customers...`;
    }); }
  deleteCustomer() {
    this.customerService
      .DeleteCustomer(this.deletedCustomerId)
      .subscribe(resp =&amp;gt; {
        this.GetCustomersFromService();
      }); }
  displayDeleteForm(custid) {this.deletedCustomerId = custid;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add following mark-up and code in the html and typescript files of the &lt;strong&gt;create-customer&lt;/strong&gt; component to render customer data entry form and functionality to make API call to save it in the database.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="container"&amp;gt;
  &amp;lt;div class="table-wrapper"&amp;gt;
    &amp;lt;div class="table-title"&amp;gt;
      &amp;lt;div class="row"&amp;gt;
        &amp;lt;div class="col-sm-6"&amp;gt;
          &amp;lt;h2&amp;gt;Customer &amp;lt;b&amp;gt;Store&amp;lt;/b&amp;gt;&amp;lt;/h2&amp;gt; &amp;lt;/div&amp;gt;
        &amp;lt;div class="col-sm-6"&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt;
    &amp;lt;!-- Add Modal HTML --&amp;gt;
    &amp;lt;div id="addCustomerModal"&amp;gt;
      &amp;lt;div class="modal-dialog"&amp;gt;
        &amp;lt;div class="modal-content"&amp;gt;
          &amp;lt;form name="addCustForm" #addCustomerForm="ngForm"&amp;gt;
            &amp;lt;div class="modal-header"&amp;gt;
              &amp;lt;h4 class="modal-title"&amp;gt;Add Customer&amp;lt;/h4&amp;gt; &amp;lt;/div&amp;gt;
            &amp;lt;div class="modal-body"&amp;gt;
              &amp;lt;div class="form-group"&amp;gt;
              &amp;lt;label&amp;gt;First Name&amp;lt;/label&amp;gt;
              &amp;lt;input type="text" name="first_name" #first_name="ngModel"
                  [(ngModel)]="customerDetail.first_name"
                  placeholder="Enter First Name" class="form-control"
                  required /&amp;gt; &amp;lt;/div&amp;gt;
              &amp;lt;div class="form-group"&amp;gt;
              &amp;lt;label&amp;gt;Last Name&amp;lt;/label&amp;gt;
              &amp;lt;input type="text" name="last_name" #last_name="ngModel"
                  [(ngModel)]="customerDetail.last_name"
                  placeholder="Enter Last Name"
                  class="form-control" required /&amp;gt; &amp;lt;/div&amp;gt;
              &amp;lt;div class="form-group"&amp;gt;
                &amp;lt;label&amp;gt;Gender&amp;lt;/label&amp;gt;
                &amp;lt;input type="text" name="gender" #gender="ngModel"
                  [(ngModel)]="customerDetail.gender"
                  placeholder="Enter Gender" class="form-control"
                  required /&amp;gt; &amp;lt;/div&amp;gt;
              &amp;lt;div class="form-group"&amp;gt;
                &amp;lt;label&amp;gt;Age&amp;lt;/label&amp;gt;
                &amp;lt;input type="text" name="age" #age="ngModel"
                  [(ngModel)]="customerDetail.age"
                  placeholder="Enter Age" class="form-control"
                  required/&amp;gt; &amp;lt;/div&amp;gt;
              &amp;lt;div class="form-group"&amp;gt;
                &amp;lt;label&amp;gt;Email&amp;lt;/label&amp;gt;
                &amp;lt;input type="text" name="email" #email="ngModel"
                  [(ngModel)]="customerDetail.email"
                  placeholder="Enter Email" class="form-control"
                  required/&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt;
            &amp;lt;div class="modal-footer"&amp;gt;
              &amp;lt;input type="button" (click)="backToHome()"
              class="btn btn-default" value="Cancel" /&amp;gt;
              &amp;lt;input type="submit" class="btn btn-success"
                [disabled]="!addCustomerForm.valid"
                (click)="addCustomer(addCustomerForm)"
                value="Add" /&amp;gt;&amp;lt;/div&amp;gt;
          &amp;lt;/form&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;

customerDetail: Customer;
  constructor( private customerService: CustomerService,
    private router: Router ) {
    this.customerDetail = {
      _id: "",
      first_name: "",
      last_name: "",
      gender: "",
      age: null,
      email: ""
    }; }
  addCustomer(addCustomerForm) {
    this.customerService.PostCustomer(addCustomerForm.value)
    .subscribe(resp =&amp;gt; {
      this.router.navigate([""]);
    }); }
  backToHome() { this.router.navigate([""]); }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now go to &lt;strong&gt;edit-customer&lt;/strong&gt; component and add following mark-up and code in the html and typescript files to render customer edit form and load selected customer data into it and make API call to update the data back in the database.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="container"&amp;gt;
  &amp;lt;div class="table-wrapper"&amp;gt;
    &amp;lt;div class="table-title"&amp;gt;
      &amp;lt;div class="row"&amp;gt;
        &amp;lt;div class="col-sm-6"&amp;gt;
          &amp;lt;h2&amp;gt;Customer &amp;lt;b&amp;gt;Store&amp;lt;/b&amp;gt;&amp;lt;/h2&amp;gt; &amp;lt;/div&amp;gt;
        &amp;lt;div class="col-sm-6"&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;!-- Edit Modal HTML --&amp;gt;
    &amp;lt;div id="updateCustomerModal"&amp;gt;
      &amp;lt;div class="modal-dialog"&amp;gt;
        &amp;lt;div class="modal-content"&amp;gt;
        &amp;lt;form name="updateCustomerForm" #updateCustomerForm="ngForm"&amp;gt;
            &amp;lt;div class="modal-header"&amp;gt;
              &amp;lt;h4 class="modal-title"&amp;gt;Edit Customer&amp;lt;/h4&amp;gt; &amp;lt;/div&amp;gt;
            &amp;lt;div class="modal-body"&amp;gt;
              &amp;lt;input type="hidden"
                name="_id" #_id="ngModel" 
                [(ngModel)]="customerDetail._id"/&amp;gt;
              &amp;lt;div class="form-group"&amp;gt;
              &amp;lt;label&amp;gt;First Name&amp;lt;/label&amp;gt;
              &amp;lt;input type="text" name="first_name" #first_name="ngModel"
                  [(ngModel)]="customerDetail.first_name"
                  placeholder="Enter First Name" class="form-control"
                  required /&amp;gt;&amp;lt;/div&amp;gt;
              &amp;lt;div class="form-group"&amp;gt;
              &amp;lt;label&amp;gt;Last Name&amp;lt;/label&amp;gt;
              &amp;lt;input type="text" name="last_name" #last_name="ngModel"
                  [(ngModel)]="customerDetail.last_name"
                  placeholder="Enter Last Name" class="form-control"
                  required /&amp;gt; &amp;lt;/div&amp;gt;
              &amp;lt;div class="form-group"&amp;gt;
                &amp;lt;label&amp;gt;Gender&amp;lt;/label&amp;gt;
                &amp;lt;input type="text" name="gender" #gender="ngModel"
                  [(ngModel)]="customerDetail.gender"
                  placeholder="Enter Gender" class="form-control"
                  required /&amp;gt; &amp;lt;/div&amp;gt;
              &amp;lt;div class="form-group"&amp;gt;
                &amp;lt;label&amp;gt;Age&amp;lt;/label&amp;gt;
                &amp;lt;input type="text" name="age" #age="ngModel"
                  [(ngModel)]="customerDetail.age"
                  placeholder="Enter Age" class="form-control"
                  required /&amp;gt; &amp;lt;/div&amp;gt;
              &amp;lt;div class="form-group"&amp;gt;
                &amp;lt;label&amp;gt;Email&amp;lt;/label&amp;gt;
                &amp;lt;input type="text" name="email" #email="ngModel"
                  [(ngModel)]="customerDetail.email"
                  placeholder="Enter Email" class="form-control"
                  required /&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt;
            &amp;lt;div class="modal-footer"&amp;gt;
              &amp;lt;input type="button" (click)="backToHome()"
                class="btn btn-default" value="Cancel" /&amp;gt;
              &amp;lt;input type="submit" class="btn btn-info"
                [disabled]="!updateCustomerForm.valid"
                (click)="updateCustomer(updateCustomerForm)"
                value="Save"/&amp;gt;&amp;lt;/div&amp;gt;
          &amp;lt;/form&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;

customerDetail: Customer;
  constructor(
    private customerService: CustomerService,
    private route: ActivatedRoute,
    private router: Router
  ) {
    this.customerDetail = {
      _id: "",
      first_name: "",
      last_name: "",
      gender: "",
      age: null,
      email: "" }; }

  ngOnInit() {this.GetCustomerFromService
(this.route.snapshot.paramMap.get("id"));}

  updateCustomer(updateCustomerForm) {
    this.customerService
      .PutCustomer(updateCustomerForm.value)
      .subscribe(resp =&amp;gt; {
        this.router.navigate([""]);
      }); }
  GetCustomerFromService(custid) {
    this.customerService.GetCustomerById(custid)
.subscribe(resp =&amp;gt; {this.customerDetail = resp as Customer;
    }); }
  backToHome() { this.router.navigate([""]); }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Open &lt;strong&gt;app&lt;/strong&gt; component html file, delete all existing mark-up code and replace it with following mark-up that will make all the routed component to be rendered in the app component.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;router-outlet&amp;gt;&amp;lt;/router-outlet&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Last, open the &lt;strong&gt;index.html&lt;/strong&gt; file and add following CSS links and scripts tag to apply bootstraps classes and functionality.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link rel="stylesheet"
 href="https://fonts.googleapis.com/css?
family=Roboto|Varela+Round"/&amp;gt;
    &amp;lt;link rel="stylesheet"
      href="https://fonts.googleapis.com/icon?
family=Material+Icons"/&amp;gt;
    &amp;lt;link rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/
font-awesome/4.7.0/css/font-awesome.min.css"/&amp;gt;
    &amp;lt;link rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/
bootstrap/3.3.7/css/bootstrap.min.css"/&amp;gt;
    &amp;lt;script src="https://ajax.googleapis.com/
ajax/libs/jquery/1.12.4/jquery.min.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src="https://maxcdn.bootstrapcdn.com/
bootstrap/3.3.7/js/bootstrap.min.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we are done with the coding so let’s run our mean stack application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run Server and Client Applications
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To run the server we need to move to mean-stack folder and run following command in the command window.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ Node server.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will launch the web server that will host our API available at &lt;strong&gt;&lt;a href="http://localhost:3000/Customers"&gt;http://localhost:3000/Customers&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;And to run the Angular client application open another command window and move into client folder and run following command.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ ng serve --open&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will open the CustomerStore client application in the browser at &lt;strong&gt;&lt;a href="http://localhost:4200"&gt;http://localhost:4200&lt;/a&gt;&lt;/strong&gt; URL like the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1YkEAmTl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/mean-stack-application-customer-store.png%3Ffit%3D665%252C355%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1YkEAmTl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/mean-stack-application-customer-store.png%3Ffit%3D665%252C355%26ssl%3D1" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you would have got fair amount of idea about developing mean stack application through this article…check out my &lt;a href="https://sanjaysaini.tech/build-restful-api-with-nodejs-express-mongodb/"&gt;article&lt;/a&gt; on building restful API as well.&lt;/p&gt;

&lt;p&gt;You can get the source code of this application from my &lt;a href="https://github.com/sanjaysaini2000/mean-stack-application"&gt;mean-stack-application&lt;/a&gt; GitHub repository.&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://sanjaysaini.tech/build-mean-stack-application/"&gt;Build Mean Stack Application&lt;/a&gt; appeared first on &lt;a href="https://sanjaysaini.tech"&gt;Sanjay Saini’s Tech World&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>node</category>
      <category>webdev</category>
      <category>vscode</category>
    </item>
    <item>
      <title>Build Restful API with Nodejs Express MongoDB</title>
      <dc:creator>Sanjay Saini</dc:creator>
      <pubDate>Mon, 20 Jan 2020 06:59:04 +0000</pubDate>
      <link>https://forem.com/sanjaysaini2000/build-restful-api-with-nodejs-express-mongodb-91g</link>
      <guid>https://forem.com/sanjaysaini2000/build-restful-api-with-nodejs-express-mongodb-91g</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d9nCsA0T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/build-restful-api-with-nodejs-express-mongodb.png%3Fresize%3D526%252C220%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d9nCsA0T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/build-restful-api-with-nodejs-express-mongodb.png%3Fresize%3D526%252C220%26ssl%3D1" alt="Build Restful API with Nodejs Express MongoDB"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article I will share how to build Restful API with Nodejs Express MongoDB that will expose endpoints to the basic CRUD operations. This restful API can be consumed by any frontend application developed in any web dev technologies.&lt;br&gt;&lt;br&gt;
I will write another article on developing frontend application and consume this restful API in that app.&lt;br&gt;&lt;br&gt;
Let’s see what we need to build this Restful API.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In order to develop and run this restful API, we need Node.js, which is JavaScript runtime installed on our machine. With Node.js comes NPM which is a node package manager that we will use to install other packages like Express needed to develop this API. So download and install the latest Node.js available from this &lt;a href="https://nodejs.org/en/"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;We need code editor as well, I will recommend VS Code but if you already have any other code editor that you are using to write C# code then stick with that otherwise download and install VS Code from this &lt;a href="https://code.visualstudio.com/download"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;We need Postman which is a famous API Client tool used in testing web API by sending web requests and inspecting response from the API. So download and install Postman for free from this &lt;a href="https://www.getpostman.com/downloads/"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Last, we need to have MongoDB installed on the machine. You can download and install MongoDB community edition free from this &lt;a href="https://www.mongodb.com/download-center/community"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  MongoDB Installation/Setup
&lt;/h3&gt;

&lt;p&gt;MongoDb is a no-SQL database that stores data in the form of JSON object which is called Document. All Documents are stored in the container called Collection. So first Collection is created and then Document in inserted into it. Download and run the installer, select Custom option and follow the wizard instructions and keep the default setting as is. It will create data folder at C:\Program Files\MongoDB\Server\4.2\data to store databases and also start MongoDB Server as window service after installation is finished. That’s all we need to do on MondoDB side for this API.&lt;br&gt;&lt;br&gt;
Now that we have setup our dev environment and installed all the required software we are ready to start and build restful API with Nodejs Express MongoDB.&lt;/p&gt;
&lt;h2&gt;
  
  
  Get Started…
&lt;/h2&gt;

&lt;p&gt;Start with creating project folder &lt;strong&gt;webapi&lt;/strong&gt; that will contain code for our restful API and move into it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ mkdir webapi&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;code&gt;$ cd webapi&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we will start with creating &lt;strong&gt;package.json&lt;/strong&gt; file which is the first building block for Node.js application and holds application configuration and package dependencies details that is required in order to run the application.&lt;br&gt;&lt;br&gt;
So open the command window in the project folder and run following command to create it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ npm init --yes&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Install Express, Mongoose and body-parser
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Express&lt;/strong&gt; is a minimal and flexible Node.js web application framework which is developed on top of Http module of Node.js that provides a robust set of features and myriad of HTTP utility methods and middleware to create a robust web API.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Mongoose&lt;/strong&gt; is a MondoDB object data modelling package that provides a straight-forward, schema-based solution to model data in the Node.js application. It includes built-in type casting, validation, query building, business logic hooks and more, out of the box.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;body-paser&lt;/strong&gt; is Node.js middleware for parsing incoming request bodies in a middleware before request handlers and is available under the req.body property.&lt;br&gt;&lt;br&gt;
So install all these packages by running following command in the command window.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ npm i express mongoose body-parser –save&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;–save option will add entry for these packages in the dependencies section in package.json file for our project.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Write Code…
&lt;/h3&gt;

&lt;p&gt;Now open the code editor to start writing code. Run following command to start VS Code editor in the project folder.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ vscode .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Create &lt;strong&gt;db.js&lt;/strong&gt; file and add following code to create and export connection with MongoDB using mongoose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");
mongoose.connect(
  "mongodb://localhost:27017/customerDb",
  { useNewUrlParser: true, useUnifiedTopology: true },
  err =&amp;gt; {
    if (!err) console.log("Successfully connect to MondoDB...");
    else
      console.log(
        "Connection to MongoDb failed :" + JSON
         .stringify(err, undefined, 2)
      );
  }
);

module.exports = mongoose;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Create &lt;strong&gt;index.js&lt;/strong&gt; file and add following code to create Express server to host our restful API. It will also import mongodb connection from db.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const bodyParser = require("body-parser");
const { mongoose } = require("./db");
const customer = require("./Controller/CustomerController");

const express = require("express");
const app = express();

//added middleware code
app.use(bodyParser.json());
app.use("/customers", customer);

const port = process.env.port || 3000;
app.listen(port, () =&amp;gt; {
  console.log(`server listening at port :${port}`);
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we need to create object data model to store Customer data in the MongoDB database. So create &lt;strong&gt;Models&lt;/strong&gt; folder and create &lt;strong&gt;Customer.js&lt;/strong&gt; file in it. Add following code to hold Customer model schema and export it as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");

var Customer = mongoose.model("Customer", {
  first_name: String,
  last_name: String,
  gender: String,
  age: Number,
  email: String
});

module.exports = { Customer };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The last piece of code is to add controller code that will expose our restful API endpoints. So create &lt;strong&gt;Controller&lt;/strong&gt; folder and in it create &lt;strong&gt;CustomerController.js&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;Now add following code for CRUD operations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add following code to handle GET request to fetch the Customers.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;router.get("/", (req, resp) =&amp;gt; {
  Customer.find((err, docs) =&amp;gt; {
    if (err)
      console.log(
        "Error while getting customers..." + JSON
         .stringify(err, undefined, 2)
      );
    else resp.send(docs);
  });
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To create new Customer, add following code to handle POST request from the client.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;router.post("/", (req, resp) =&amp;gt; {
  let cust = new Customer({
    first_name: req.body.first_name,
    last_name: req.body.last_name,
    gender: req.body.gender,
    age: req.body.age,
    email: req.body.email
  });
  cust.save((err, doc) =&amp;gt; {
    if (err)
      console.log(
        "error in saving customers..." + JSON
         .stringify(err, undefined, 2)
      );
    else resp.send(doc);
  });
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add following code to handle PUT request from client to update existing Customer.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;router.put("/:id", (req, resp) =&amp;gt; {
  let customerId = req.params.id;
  if (!ObjectId.isValid(customerId))
    return resp.status(400)
               .send(`Customer not found for id :${customerId}`);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;And to handle DELETE request to delete the Customer, Add following code.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;router.delete("/:id", (req, resp) =&amp;gt; {
  let customerId = req.params.id;
  if (!ObjectId.isValid(customerId))
    return resp.status(400)
               .send(`Customer not found for id :${customerId}`);

  Customer.deleteOne({ _id: customerId }, (err, docs) =&amp;gt; {
    if (err)
      console.log(
        "Error while deleting customers..." + JSON
         .stringify(err, undefined, 2)
      );
    else resp.send(docs);
  });
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That’s all we need to code for now…&lt;/p&gt;

&lt;h3&gt;
  
  
  Launch the API
&lt;/h3&gt;

&lt;p&gt;Now run following command to host our restful API.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ node index.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will launch the web server that will host our API available at &lt;strong&gt;&lt;a href="http://localhost:3000/Customers"&gt;http://localhost:3000/Customers&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Test the API
&lt;/h3&gt;

&lt;p&gt;Now open the Postman Web API Client application and click on the &lt;strong&gt;Create a request&lt;/strong&gt; to initiate an API request to our restful API.&lt;/p&gt;

&lt;p&gt;Take following steps to test POST request handler to create the Customer.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select &lt;strong&gt;POST&lt;/strong&gt; HTTP verb from the drop down.&lt;/li&gt;
&lt;li&gt;Enter request URL &lt;strong&gt;&lt;a href="http://localhost:3000/Customers"&gt;http://localhost:3000/Customers&lt;/a&gt;&lt;/strong&gt; for post request.&lt;/li&gt;
&lt;li&gt;Select Body tab and then select &lt;strong&gt;raw&lt;/strong&gt; radio button and finally select type &lt;strong&gt;JSON&lt;/strong&gt; from the drop down.&lt;/li&gt;
&lt;li&gt;In the request body enter following JSON object representing our Customer resource that we want to create.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "first_name" : "Amit",
  "last_name" : "Patil",
  "gender" : "Male",
  "age" : 39,
  "email" : "Amit@gmail.com"
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can test rest of the CRUD operations by following my &lt;a href="https://sanjaysaini.tech/create-asp-net-core-3-web-api-using-entity-framework-core/"&gt;article&lt;/a&gt; where I have tested another web api using PostMan. Although web api is different but steps to test any we api are same with PostMan.&lt;/p&gt;

&lt;p&gt;Hope you have enjoyed it…check out my other articles.&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://sanjaysaini.tech/build-restful-api-with-nodejs-express-mongodb/"&gt;Build Restful API with Nodejs Express MongoDB&lt;/a&gt; appeared first on &lt;a href="https://sanjaysaini.tech"&gt;Sanjay Saini’s Tech World&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>vscode</category>
    </item>
    <item>
      <title>Create JavaScript Frontend for Web API CRUD operations.</title>
      <dc:creator>Sanjay Saini</dc:creator>
      <pubDate>Sat, 11 Jan 2020 11:25:46 +0000</pubDate>
      <link>https://forem.com/sanjaysaini2000/create-javascript-frontend-for-web-api-crud-operations-4gnh</link>
      <guid>https://forem.com/sanjaysaini2000/create-javascript-frontend-for-web-api-crud-operations-4gnh</guid>
      <description>&lt;p&gt;In this article I will show how to create JavaScript Frontend for Web API CRUD operations using Bootstrap and some simple javascript code as shown in the below image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7cd4Omn---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Javascript-frontend-for-webapi.png%3Fw%3D665%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7cd4Omn---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Javascript-frontend-for-webapi.png%3Fw%3D665%26ssl%3D1" alt="Create JavaScript Frontend for Web API CRUD operations"&gt;&lt;/a&gt;JavaScript Frontend for BookStore Web API CRUD operations&lt;/p&gt;

&lt;p&gt;Some time back I had written BookStore Web API in ASP.Net Core that perform simple CRUD operations using Entity Framework Core and uses SQL Server Express edition as back-end for data storage and retrieval.&lt;/p&gt;

&lt;p&gt;Check out my &lt;a href="https://sanjaysaini.tech/create-asp-net-core-3-web-api-using-entity-framework-core/"&gt;article&lt;/a&gt; on creating this BookStore Web API for which I will be sharing how to create javascript frontend in this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In order to run the BookStore Web API you will need .Net Core SDK that includes .Net CLI tools, .Net Core Runtime. So download and install the latest version of .Net Core SDK available from this &lt;a href="https://dotnet.microsoft.com/download"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;We will also need code editor, I will recommend VS Code but if you already have any other code editor that you are using to write C# code then stick with that otherwise download and install VS Code from this &lt;a href="https://code.visualstudio.com/download"&gt;link&lt;/a&gt;. After installing SDK and VS Code, open the command prompt and run following command to check if SDK installed properly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ dotnet –version&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You must get the latest version number as a result. For me its 3.1.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;And last, we need to have SQL Server installed on the machine. In case you already have Visual Studio 2015 or later installed then SQL Server Express edition will already be installed along with the Visual Studio. Otherwise download and install latest SQL Server Express edition free from this &lt;a href="https://www.microsoft.com/en-us/sql-server/sql-server-editions-express"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After done with the environment setup we are ready to create JavaScript Frontend for our BookStore Web API CRUD operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Started…
&lt;/h2&gt;

&lt;p&gt;Here we need few things before we actually start writing some html/javascript code to develop the frontend.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, we need BookStore Web API code on top of which we will develop frontend.&lt;/li&gt;
&lt;li&gt;Second, we need to create scaffolding to contain frontend code.&lt;/li&gt;
&lt;li&gt;Finally, get some readymade html/css/javascript code for frontend by using Bootstrap template for Data table and Modal Forms. So let’s get started…&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Get Source code
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Get the source code of BookStore Web API from my GitHub repository &lt;a href="https://github.com/sanjaysaini2000/aspnet-core3-webapi"&gt;link&lt;/a&gt; by downloading the ZIP file from “Clone and download” button and unzip it in the folder on your machine.&lt;/li&gt;
&lt;li&gt;Follow the instructions in the &lt;strong&gt;README.md&lt;/strong&gt; file to setup the code for further development.&lt;/li&gt;
&lt;li&gt;Open the command window in the folder where you unzipped the source code and start &lt;strong&gt;VS Code&lt;/strong&gt; editor by running following command in the command window.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ vscode .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pPu8Q3iV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Javascript-frontend-for-webapi2.png%3Fw%3D665%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pPu8Q3iV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Javascript-frontend-for-webapi2.png%3Fw%3D665%26ssl%3D1" alt="Create JavaScript Frontend for Web API CRUD operations"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Before we begin developing frontend we need to make few minor changes in our Web API code application in order to make it work like a website.

&lt;ul&gt;
&lt;li&gt;First, we need to force the application to open the &lt;strong&gt;index.html&lt;/strong&gt; instead of Web API so open the &lt;strong&gt;Properties\launchSettings.json&lt;/strong&gt; file and remove the &lt;em&gt;launchUrl&lt;/em&gt; property.&lt;/li&gt;
&lt;li&gt;Second, we want to make sure our application serves static files (html/css/javascript) to the web client and also serve the default file (in our case index.html) when site loads into browser. So add following code in the &lt;em&gt;Configure&lt;/em&gt; method of &lt;strong&gt;Startup.cs&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.UseDefaultFiles();
app.UseStaticFiles();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Create JavaScript Frontend
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;We will start with creating scaffolding for our frontend. So create following file/folder structure that will contain all our frontend code.

&lt;ul&gt;
&lt;li&gt;Create &lt;strong&gt;wwwroot&lt;/strong&gt; folder at the root folder of the application.&lt;/li&gt;
&lt;li&gt;Create &lt;strong&gt;index.html&lt;/strong&gt; file in the &lt;strong&gt;wwwroot&lt;/strong&gt; folder.&lt;/li&gt;
&lt;li&gt;Create &lt;strong&gt;js&lt;/strong&gt; folder in the &lt;strong&gt;wwwroot&lt;/strong&gt; folder.&lt;/li&gt;
&lt;li&gt;Create &lt;strong&gt;site.js&lt;/strong&gt; folder in the &lt;strong&gt;js&lt;/strong&gt; folder.&lt;/li&gt;
&lt;li&gt;Create &lt;strong&gt;css&lt;/strong&gt; folder in the &lt;strong&gt;wwwroot&lt;/strong&gt; folder.&lt;/li&gt;
&lt;li&gt;Create &lt;strong&gt;site.css&lt;/strong&gt; folder in the &lt;strong&gt;css&lt;/strong&gt; folder.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Nq660VWY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Javascript-frontend-for-webapi3.png%3Fw%3D665%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Nq660VWY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Javascript-frontend-for-webapi3.png%3Fw%3D665%26ssl%3D1" alt="Create JavaScript Frontend for Web API CRUD operations"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We will not write all the html/CSS code for our frontend instead we will get some readymade html/CSS code for showing Data Table with Modal Form for CRUD operations by using Bootstrap template for Data Table with Modal Form.
So to get the Bootstrap template, open this &lt;a href="https://www.tutorialrepublic.com/snippets/preview.php?topic=bootstrap&amp;amp;file=crud-data-table-for-database-with-modal-form"&gt;link&lt;/a&gt; and click on &lt;strong&gt;view code&lt;/strong&gt; and copy the source code in the index.html file.&lt;/li&gt;
&lt;li&gt;Remove all “checkbox” elements and pagination related code from the index.html since we just want to focus on CRUD operations related UI right now.&lt;/li&gt;
&lt;li&gt;Cut out all the CSS code between &lt;em&gt;&amp;lt;style type="text/css"&amp;gt;&lt;/em&gt; tag from the index.html file and paste it in the site.css file. And add following code in the &lt;em&gt;&amp;lt;head&amp;gt;&lt;/em&gt; section of index.html.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;link rel="stylesheet" href="css/site.css" /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Although we could keep the CSS code in the index.html file but as a best practice we should always keep all style related code in the separate folder.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Code change for Web API CRUD Operations
&lt;/h3&gt;

&lt;p&gt;Now we will make html/javascript code changes to integrate Web API CRUD operations like getting/showing the data, creating, updating and deleting the data resource through frontend.&lt;/p&gt;

&lt;h5&gt;
  
  
  Code change for getting data and showing it on the data table in UI.
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Remove all code between &lt;em&gt;&amp;lt;table class="table table-striped table-hover w-auto"&amp;gt;&lt;/em&gt; tag and replace it with following code in the index.html file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;thead&amp;gt;
            &amp;lt;tr&amp;gt;
              &amp;lt;th&amp;gt;Title&amp;lt;/th&amp;gt;
              &amp;lt;th&amp;gt;Author&amp;lt;/th&amp;gt;
              &amp;lt;th&amp;gt;Publisher&amp;lt;/th&amp;gt;
              &amp;lt;th&amp;gt;Genre&amp;lt;/th&amp;gt;
              &amp;lt;th&amp;gt;Price&amp;lt;/th&amp;gt;
              &amp;lt;th&amp;gt;Actions&amp;lt;/th&amp;gt;
            &amp;lt;/tr&amp;gt;
          &amp;lt;/thead&amp;gt;
          &amp;lt;tbody id="books"&amp;gt;&amp;lt;/tbody&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add following code under &lt;em&gt;&amp;lt;div class="clearfix"&amp;gt;&lt;/em&gt; to show the number of records.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="counter" class="hint-text"&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Also add following javascript tag just above the &lt;em&gt;&amp;lt;/body&amp;gt;&lt;/em&gt; tag in the index.html file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="js/site.js" asp-append-version="true"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;script type="text/javascript"&amp;gt;
 getBookItems();
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add following javascript code in the site.js file that will make the Web API GET call to fetch the data from database and display it on the UI.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const uri = "api/BookItems";
let books = [];

function getBookItems() {
  fetch(uri)
    .then(response =&amp;gt; response.json())
    .then(data =&amp;gt; _displayItems(data))
    .catch(error =&amp;gt; console.error("Unable to get books.", error));
}

function displayDeleteForm(id) {
  const item = books.find(item =&amp;gt; item.id === id);
  document.getElementById("delete-id").value = item.id;
}

function displayEditForm(id) {
  const item = books.find(item =&amp;gt; item.id === id);
  document.getElementById("edit-id").value = item.id;
  document.getElementById("edit-title").value = item.title;
  document.getElementById("edit-author").value = item.author;
  document.getElementById("edit-publisher").value = item.publisher;
  document.getElementById("edit-genre").value = item.genre;
  document.getElementById("edit-price").value = item.price;
}

function _displayCount(itemCount) {
  const name = itemCount === 1 ? "entry" : "entries";
  document.getElementById(
    "counter"
  ).innerHTML = `Showing &amp;lt;b&amp;gt;${itemCount}&amp;lt;/b&amp;gt; ${name}`;
}

function _displayItems(data) {
  const tBody = document.getElementById("books");
  tBody.innerHTML = "";
  _displayCount(data.length);
  const button = document.createElement("button");

  data.forEach(item =&amp;gt; {
    let editButton = document.createElement("a");
    editButton.href = "#editBookModal";
    editButton.className = "edit";
    editButton.setAttribute("onclick", `displayEditForm(${item.id})`);
    editButton.setAttribute("data-toggle", "modal");
    editButton.innerHTML =
      "&amp;lt;i class='material-icons' data-toggle='tooltip' title='Edit'&amp;gt;&amp;amp;#xE254;&amp;lt;/i&amp;gt;";

    let deleteButton = document.createElement("a");
    deleteButton.href = "#deleteBookModal";
    deleteButton.className = "delete";
    deleteButton.setAttribute("onclick", `displayDeleteForm(${item.id})`);
    deleteButton.setAttribute("data-toggle", "modal");
    deleteButton.innerHTML =
      "&amp;lt;i class='material-icons' data-toggle='tooltip' title='Delete'&amp;gt;&amp;amp;#xE872;&amp;lt;/i&amp;gt;";

    let tr = tBody.insertRow();

    let td1 = tr.insertCell(0);
    let textTitle = document.createTextNode(item.title);

    td1.appendChild(textTitle);

    let td2 = tr.insertCell(1);
    let textAuthor = document.createTextNode(item.author);
    td2.appendChild(textAuthor);

    let td3 = tr.insertCell(2);
    let textPublisher = document.createTextNode(item.publisher);
    td3.appendChild(textPublisher);

    let td4 = tr.insertCell(3);
    let textGenre = document.createTextNode(item.genre);
    td4.appendChild(textGenre);

    let td5 = tr.insertCell(4);
    let textPrice = document.createTextNode(item.price);
    td5.appendChild(textPrice);

    let td6 = tr.insertCell(5);
    td6.appendChild(editButton);
    td6.appendChild(deleteButton);
  });

  books = data;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Code change for Add Book Model form and creating/saving the data.
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Replace code in the &lt;em&gt;&amp;lt;div class="col-sm-6"&amp;gt;&lt;/em&gt; tag with the following code to show “Add New Book” modal form.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a
                href="#addBookModal"
                class="btn btn-success"
                data-toggle="modal"
                &amp;gt;&amp;lt;i class="material-icons"&amp;gt;&amp;amp;#xE147;&amp;lt;/i&amp;gt;
                &amp;lt;span&amp;gt;Add New Book&amp;lt;/span&amp;gt;&amp;lt;/a
              &amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Replace the top level div id with “addBookModal” under the &lt;em&gt;&amp;lt;!– Add Modal HTML –&amp;gt;&lt;/em&gt; section that contains html code for Add Modal Form.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Replace &lt;em&gt;&amp;lt;form&amp;gt;&lt;/em&gt; tag with following code.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form action="javascript:void(0);" method="POST" onsubmit="addBookItem()"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Also edit the existing &lt;em&gt;&amp;lt;label&amp;gt;&lt;/em&gt; to show Title, Author, Publisher, Genre and Price.&lt;/li&gt;
&lt;li&gt;And edit &lt;em&gt;&amp;lt;input&amp;gt;&lt;/em&gt; tags with following &lt;em&gt;&amp;lt;input&amp;gt;&lt;/em&gt; tags so the end result should look like the form below.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="text" id="add-title" placeholder="Enter Book Title"
class="form-control" required /&amp;gt;
&amp;lt;input type="text" id="add-author" placeholder="Enter Book Author"
class="form-control" required /&amp;gt;
&amp;lt;input type="text" id="add-publisher" placeholder="Enter Book Publisher"
class="form-control" required /&amp;gt;
&amp;lt;input type="text" id="add-genre" placeholder="Enter Book Genre"
class="form-control" required /&amp;gt;
&amp;lt;input type="text" id="add-price" placeholder="Enter Book Price"
class="form-control" required /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MOmr8Gxq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Javascript-frontend-for-webapi-add-form.png%3Fresize%3D455%252C624%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MOmr8Gxq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Javascript-frontend-for-webapi-add-form.png%3Fresize%3D455%252C624%26ssl%3D1" alt="Create JavaScript Frontend for Web API CRUD operations"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add following javascript code in the site.js that will make Web API POST call to save/create the data.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addBookItem() {
  const titleInputText = document.getElementById("add-title");
  const auhtorInputText = document.getElementById("add-author");
  const publisherInputText = document.getElementById("add-publisher");
  const genreInputText = document.getElementById("add-genre");
  const priceInputText = document.getElementById("add-price");

  const item = {
    title: titleInputText.value.trim(),
    author: auhtorInputText.value.trim(),
    publisher: publisherInputText.value.trim(),
    genre: genreInputText.value.trim(),
    price: parseInt(priceInputText.value.trim())
  };
  console.log(JSON.stringify(item));
  fetch(uri, {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    body: JSON.stringify(item)
  })
    .then(response =&amp;gt; response.json())
    .then(() =&amp;gt; {
      getBookItems();
      titleInputText.value = "";
      auhtorInputText.value = "";
      publisherInputText.value = "";
      genreInputText.value = "";
      priceInputText.value = "";
    })
    .catch(error =&amp;gt; console.error("Unable to add Book.", error));
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Code change for Edit Book Model form and updating the data.
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Under the &lt;em&gt;&amp;lt;!– Edit Modal HTML –&amp;gt;&lt;/em&gt; section that contains html code for Edit Modal Form, replace the top level div id with “editBookModal”.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Replace &lt;em&gt;&amp;lt;form&amp;gt;&lt;/em&gt; tag with following code.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form action="javascript:void(0);" onsubmit="updateBookItem()"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Also edit the existing &lt;em&gt;&amp;lt;label&amp;gt;&lt;/em&gt; to show Title, Author, Publisher, Genre and Price.&lt;/li&gt;
&lt;li&gt;And edit &lt;em&gt;&amp;lt;input&amp;gt;&lt;/em&gt; tags the same way we did for “Add Book” form with the &lt;em&gt;&amp;lt;input&amp;gt;&lt;/em&gt; tags code we used there but make sure the &lt;strong&gt;id&lt;/strong&gt; value of input tags should be prefixed with &lt;em&gt;edit&lt;/em&gt; instead of &lt;em&gt;add&lt;/em&gt; this time so the end result should look like the form below.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JeNS1HZR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Javascript-frontend-for-webapi-edit-form.png%3Fresize%3D454%252C613%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JeNS1HZR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Javascript-frontend-for-webapi-edit-form.png%3Fresize%3D454%252C613%26ssl%3D1" alt="Create JavaScript Frontend for Web API CRUD operations"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add following javascript code in the site.js that will make Web API PUT call to update the data.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function updateBookItem() {
  const itemId = document.getElementById("edit-id").value.trim();
  const item = {
    id: parseInt(itemId, 10),
    title: document.getElementById("edit-title").value.trim(),
    author: document.getElementById("edit-author").value.trim(),
    publisher: document.getElementById("edit-publisher").value.trim(),
    genre: document.getElementById("edit-genre").value.trim(),
    price: parseInt(document.getElementById("edit-price").value.trim())
  };

  fetch(`${uri}/${itemId}`, {
    method: "PUT",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    body: JSON.stringify(item)
  })
    .then(() =&amp;gt; getBookItems())
    .catch(error =&amp;gt; console.error("Unable to update item.", error));

  return false;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Code change for Delete Book Model form and deleting the data.
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Finally, under the &lt;em&gt;&amp;lt;!– Delete Modal HTML –&amp;gt;&lt;/em&gt; section that contains html code for Delete Modal Form, replace the top level div id with “deleteBookModal”.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And replace &lt;em&gt;&amp;lt;form&amp;gt;&lt;/em&gt; tag with following code.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form action="javascript:void(0);" onsubmit="deleteBookItem()"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Also change the text under H4 tag that will appear as form title for all the forms accordingly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4CW2euFC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Javascript-frontend-for-webapi-delete-form.png%3Fresize%3D454%252C275%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4CW2euFC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/sanjaysaini.tech/wp-content/uploads/2020/01/Javascript-frontend-for-webapi-delete-form.png%3Fresize%3D454%252C275%26ssl%3D1" alt="Create JavaScript Frontend for Web API CRUD operations"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add following javascript code in the site.js that will make Web API DELETE call to delete the data.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function deleteBookItem() {
  const itemId = document.getElementById("delete-id").value.trim();
  fetch(`${uri}/${itemId}`, {
    method: "DELETE"
  })
    .then(() =&amp;gt; getBookItems())
    .catch(error =&amp;gt; console.error("Unable to delete Book.", error));
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Finally add following jqury code under &lt;em&gt;&amp;lt;script type="text/javascript"&amp;gt;&lt;/em&gt; tag in the index.html file to force the Modal Forms to close when we submit them.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$("#addBookModal").submit(function() {
        $("#addBookModal").modal("hide");
      });

      $("#editBookModal").submit(function() {
        $("#editBookModal").modal("hide");
      });

      $("#deleteBookModal").submit(function() {
        $("#deleteBookModal").modal("hide");
      });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I have covered all major code changes in this article to implement the CRUD operations however if I missed any cosmetic changes like label text etc. then try to fix them yourself.&lt;/p&gt;

&lt;p&gt;OR&lt;/p&gt;

&lt;p&gt;You can get the complete source code of what we have developed here from this &lt;a href="https://github.com/sanjaysaini2000/aspnet-core3-webapi/tree/frontend"&gt;link&lt;/a&gt; of my GitHub repository.&lt;/p&gt;

&lt;p&gt;Keep learning and…subscribe to my blog if you liked this article.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>vscode</category>
    </item>
    <item>
      <title>Create ASP.Net Core 3 Web API Using Entity Framework Core</title>
      <dc:creator>Sanjay Saini</dc:creator>
      <pubDate>Thu, 02 Jan 2020 09:43:47 +0000</pubDate>
      <link>https://forem.com/sanjaysaini2000/create-asp-net-core-3-web-api-using-entity-framework-core-46n9</link>
      <guid>https://forem.com/sanjaysaini2000/create-asp-net-core-3-web-api-using-entity-framework-core-46n9</guid>
      <description>&lt;p&gt;In this &lt;a href="https://sanjaysaini.tech/create-asp-net-core-3-web-api-using-entity-framework-core/" rel="noopener noreferrer"&gt;article&lt;/a&gt; (originally published at &lt;a href="https://sanjaysaini.tech/" rel="noopener noreferrer"&gt;sanjaysaini.tech&lt;/a&gt;) I will be sharing how we can create ASP.Net Core 3 Web API using Entity Framework Core edition which is basically an open source and cross-platform version of the Entity Framework to be used in the .Net Core application development.&lt;br&gt;
This Web API will perform simple CRUD operations using Entity Framework Core and will use SQL Server Express edition as backend for data storage and retrieval.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In order to develop and run .Net Core app, we need .Net Core SDK that includes .Net CLI tools, .Net Core Runtime.
So download and install the latest version of .Net Core SDK available from this &lt;a href="https://dotnet.microsoft.com/download" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;We will also need code editor, I will recommend VS Code but if you already have any other code editor that you are using to write C# code then stick with that otherwise download and install VS Code from this &lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;link&lt;/a&gt;.
After installing SDK and VS Code, open the command prompt and run following command to check if SDK installed properly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ dotnet –version&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
You must get the latest version number as a result. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To test this web API’s CRUD operations, we need to install &lt;strong&gt;Postman&lt;/strong&gt; which a famous API Client tool used in testing web API by sending web requests and inspecting response from the API.
So download and install Postman for free from this &lt;a href="https://www.getpostman.com/downloads/" rel="noopener noreferrer"&gt;link&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;And last, we need to have SQL Server installed on the machine. In case you already have Visual Studio 2015 or later installed then SQL Server Express edition will already be installed along with the Visual Studio.
Otherwise download and install latest SQL Server Express edition free from this &lt;a href="https://www.microsoft.com/en-us/sql-server/sql-server-editions-express" rel="noopener noreferrer"&gt;link&lt;/a&gt;.
After done with the environment setup we are ready to create ASP.Net Core 3 Web API using Entity Framework Core which will provide CRUD operations.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Create a new project
&lt;/h2&gt;

&lt;p&gt;For the purpose of this article I am creating a BookStore Web API that will provide CRUD operation on web requests (Post, Get, Put and Delete) for Book data Model. &lt;br&gt;
So now go to the folder location where you want to create Web API project and open the command window. &lt;br&gt;
Create &lt;em&gt;webapi&lt;/em&gt; type project for our CRUD Web API by running following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet new webapi -o BookStoreApi&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will create BookStoreApi folder with the same name project file in it. &lt;/p&gt;

&lt;p&gt;Now we need to work under this folder so move into this folder. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ cd BookStoreApi&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fr62ty4g2psbgde5jqeka.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fr62ty4g2psbgde5jqeka.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our Web API will listening at &lt;strong&gt;HTTPS&lt;/strong&gt; so we need to create and trust the development certificate so that web API hosts securely on local development server when we run the project.&lt;/p&gt;

&lt;p&gt;So run following command and click on the &lt;em&gt;yes&lt;/em&gt; button on the &lt;strong&gt;Security Warning&lt;/strong&gt; dialog window.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dotnet dev-certs https --trust&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Add Entity Framework Core Package Reference
&lt;/h2&gt;

&lt;p&gt;First we need to install Entity Framework Core and for that we need to install the package for the EF Core database provider that we want to target. This Web API will use SQL Server so run following command to install the Entity Framework Core.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet add package Microsoft.EntityFrameworkCore.SqlServer&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You find following entry in the app project file after successful installation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;ItemGroup&amp;gt;&lt;br&gt;
    &amp;lt;PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" /&amp;gt;&lt;br&gt;
  &amp;lt;/ItemGroup&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Check out my &lt;a href="https://sanjaysaini.tech/create-crud-application-using-entity-framework-core-3/" rel="noopener noreferrer"&gt;article&lt;/a&gt; on creating CRUD application using Entity Framework Core 3 as well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As we are done with our Web API project setup, now open VS code in the project folder to write some code.&lt;/p&gt;

&lt;p&gt;Run following command in the command window to open the VS Code.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ code .&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Create the model
&lt;/h2&gt;

&lt;p&gt;Model represents the set of classes that an application uses to manage the data.&lt;br&gt;
Our model will have a POCO class to manage data for our Book Store API.&lt;br&gt;
We should keep all our model classes in the &lt;strong&gt;Models&lt;/strong&gt; folders so let’s create Models folder and create &lt;strong&gt;BookItem.cs&lt;/strong&gt; class in it that will represent Book entities in our model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public class BookItem
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public string Publisher { get; set; }
        public string Genre { get; set; }   
        public int Price { get; set; }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also create** BookStoreDbContext.cs** class derived from DbContext that represents session with the database in the Models folder. This class object will be used to perform CRUD operations on the database and its properties will represent table in the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.EntityFrameworkCore;

public class BookStoreDbContext: DbContext
{
     public BookStoreDbContext (DbContextOptions&amp;lt; BookStoreDbContext &amp;gt; options) : base(options)
     { }

    public DbSet&amp;lt; BookItem&amp;gt; BookItems { get; set; }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Register Database Context
&lt;/h2&gt;

&lt;p&gt;We have to register our BookStoreDbContext with the Dependency Injection container that provides services to API controllers.&lt;br&gt;
And also need to configure the database connection that will be used by the Entity Framework Migrations to sync that database with the data model in the application.&lt;br&gt;
So add following code in the &lt;em&gt;ConfigureServices&lt;/em&gt; method of &lt;strong&gt;Startup.cs&lt;/strong&gt; class to pass the database connection while adding the DbContext service to the container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;services.AddDbContext&amp;lt; BookStoreDbContext &amp;gt;(opt =&amp;gt; opt
 .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=BookStoreApiDb;Trusted_Connection=True;"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also add following references in the &lt;strong&gt;Startup.cs&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.EntityFrameworkCore;
using BookStoreApi.Models;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Entity Framework Migrations
&lt;/h2&gt;

&lt;p&gt;Entity Framework provides a way to create a database from the data model using Migrations. Migrations are used to keen database in sync with the changes in the application’s data model by updating the database schema while preserving existing data in the database.&lt;/p&gt;

&lt;p&gt;So in order to create migrations we need to install Entity Framework Core tools that includes &lt;strong&gt;dotnet ef&lt;/strong&gt; tool and the Entity Framework designer.&lt;/p&gt;

&lt;p&gt;Run following command in the command window to install the Entity Framework Core tools.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet tool install --global dotnet-ef&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;if you have already installed the Entity Framework Core tools then don’t install it again.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now run following command to add Entity Framework designer package to the project.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet add package Microsoft.EntityFrameworkCore.Design&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will add following entry in the application project file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0"&amp;gt;&lt;br&gt;
&amp;lt;IncludeAssets&amp;gt;runtime; build; native; contentfiles; analyzers; buildtransitive&amp;lt;/IncludeAssets&amp;gt;&lt;br&gt;
&amp;lt;PrivateAssets&amp;gt;all&amp;lt;/PrivateAssets&amp;gt;&lt;br&gt;
&amp;lt;/PackageReference&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Create the database
&lt;/h2&gt;

&lt;p&gt;Since we have installed Entity Framework Core tools, we are ready to create database from our application data model.&lt;br&gt;
First, we need to add migrations that will create the database schema of our model entities. &lt;br&gt;
So run following command to create the first migration.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet ef migrations add InitialCreate&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fs96wr5xi5xcpuco4ds8h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fs96wr5xi5xcpuco4ds8h.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It will add Migrations folder in the project with auto-generated files for &lt;strong&gt;InitialCreate&lt;/strong&gt; migration.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Second, we need to apply &lt;strong&gt;InitialCreate&lt;/strong&gt; migration on the database. Since it’s a very first migration and we don’t have existing database so it will create new database for us.&lt;/p&gt;

&lt;p&gt;So run following command to apply the migration to the database.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet ef database update&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwosc21d6cw7q8w6zfgmq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwosc21d6cw7q8w6zfgmq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A new database BookStoreApiDb is created along with the table BookItems for the Model entity.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Add API Controller
&lt;/h2&gt;

&lt;p&gt;Unlike Visual Studio that has template to generate boiler plate code for Web API controller, VS Code editor doesn’t provide any template. &lt;br&gt;
So we need to install &lt;strong&gt;aspnet code generator tool&lt;/strong&gt; to add API controller in our application.&lt;br&gt;&lt;br&gt;
Run following command in the command window to install the aspnet code generator tool.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet tool install --global dotnet-aspnet-codegenerator&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Also add code generation design package in the project by running following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will add following entry in the application project file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" /&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Since we have code generation tool installed, now we can add API controller &lt;em&gt;BookItemsController&lt;/em&gt; which will use &lt;em&gt;BookItem&lt;/em&gt; Model and &lt;em&gt;BookStoreDbContext&lt;/em&gt; database context.&lt;/p&gt;

&lt;p&gt;So run following command to generate the API controller.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet aspnet-codegenerator controller -name BookItemsController -async -api -m BookItem -dc BookStoreDbContext  -outDir Controllers&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F37hck902xiwan2kkji6l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F37hck902xiwan2kkji6l.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above command will create &lt;strong&gt;BookItemsController.cs&lt;/strong&gt; controller class in the Controllers folder with auto generated CRUD operation code (action methods) for following HTTP verbs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;HttpGet – to get the web resources/data&lt;br&gt;
HttpPost – to create web resource/save data&lt;br&gt;
HttpPut – to update web resource/data&lt;br&gt;
HttpDelete – to delete web resource/data&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Test CRUD Operations with Postman
&lt;/h2&gt;

&lt;p&gt;After completing the initial development of our web API, we are ready to test it.&lt;br&gt;
So run the following command to start up our BookStore web API.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet run&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will host Web API locally and it will be listening at &lt;strong&gt;&lt;a href="https://localhost:5001" rel="noopener noreferrer"&gt;https://localhost:5001&lt;/a&gt;&lt;/strong&gt; URL. However to request web resource to the API we need to append this URL with the specific action method end point and use it with specific Http verb.&lt;/p&gt;

&lt;p&gt;For example to get the web resource we need to use following URL with GET HTTP verb in the Web API Client or web browser.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="https://localhost:5001/api/BookItems" rel="noopener noreferrer"&gt;https://localhost:5001/api/BookItems&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now open the Postman Web API Client application and click on the &lt;strong&gt;Create a request&lt;/strong&gt; to initiate an API request to our BookStore web API.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fj7d5ql0ujtnxp1dj3k2a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fj7d5ql0ujtnxp1dj3k2a.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Create Web Resource/Data Operation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Select &lt;strong&gt;POST&lt;/strong&gt; HTTP verb from the drop down.&lt;/li&gt;
&lt;li&gt;Enter request URL &lt;strong&gt;&lt;a href="https://localhost:5001/api/BookItems" rel="noopener noreferrer"&gt;https://localhost:5001/api/BookItems&lt;/a&gt;&lt;/strong&gt; for post request.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Body&lt;/strong&gt; tab and then select &lt;strong&gt;raw&lt;/strong&gt; radio button and finally select type &lt;strong&gt;JSON&lt;/strong&gt; from the drop down.
&lt;/li&gt;
&lt;li&gt;In the request body enter following JSON object representing our BookItem resource that we want to create.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Title":"The Art of War",
    "Author":"Sun Tzu",
    "Publisher":"British Museum",
    "Price": 11
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;We are not passing “id” value since it will be auto generated by the Entity Framework.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Now click on Send button.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbtejdw7r6hf4spmay07n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbtejdw7r6hf4spmay07n.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now click on the &lt;strong&gt;Header&lt;/strong&gt; tab and check the status that should be &lt;strong&gt;201 Created&lt;/strong&gt; which means the post request successfully created the BookItem resource.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F10edmpc8wzid2m5tabw6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F10edmpc8wzid2m5tabw6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create another BookItem resource the same way using following JSON object in the request body.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Title":"The Monk Who Sold His Ferrari",
    "Author":"Robin Sharma",
    "Publisher":"Jaico Publishing",
    "Genre":"Fiction",
    "Price": 121
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Read Web Resource/Data Operation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Select &lt;strong&gt;GET&lt;/strong&gt; HTTP verb from the drop down.&lt;/li&gt;
&lt;li&gt;Enter request URL  &lt;strong&gt;&lt;a href="https://localhost:5001/api/BookItems" rel="noopener noreferrer"&gt;https://localhost:5001/api/BookItems&lt;/a&gt;&lt;/strong&gt; for get request. &lt;/li&gt;
&lt;li&gt;Click on the &lt;strong&gt;Send&lt;/strong&gt; button.&lt;/li&gt;
&lt;li&gt;Now click on the &lt;strong&gt;Header&lt;/strong&gt; tab and check the response &lt;strong&gt;body&lt;/strong&gt; should be showing the resources we had created.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fqjueah4ukhnqvmi3rkrt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fqjueah4ukhnqvmi3rkrt.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Update Web Resource/Data Operation
&lt;/h3&gt;

&lt;p&gt;Now we want to update the first BookItem resource that we created because we forgot to pass &lt;strong&gt;Genre&lt;/strong&gt; value when we created it so now we will update it with the Genre value.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select &lt;strong&gt;PUT&lt;/strong&gt; HTTP verb from the drop down.&lt;/li&gt;
&lt;li&gt;Enter request URL &lt;strong&gt;&lt;a href="https://localhost:5001/api/BookItems/1" rel="noopener noreferrer"&gt;https://localhost:5001/api/BookItems/1&lt;/a&gt;&lt;/strong&gt; for put request for first BookItem resource (id=1). &lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Body&lt;/strong&gt; tab and then select &lt;strong&gt;raw&lt;/strong&gt; radio button and finally select type &lt;strong&gt;JSON&lt;/strong&gt; from the dropdown.
&lt;/li&gt;
&lt;li&gt;In the request body enter following JSON object representing the first BookItem resource that we want to update.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "id": 1,    
    "title": "The Art of War",
    "author": "Sun Tzu",
    "publisher": "British Museum",
    "genre": "History",
    "price": 11
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;this time we are passing “id” value in body as well as in the PUT request URL to specify the resource that we want to update.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now click on &lt;strong&gt;Send&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fsj5lz2cbnhab7e6fue8c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fsj5lz2cbnhab7e6fue8c.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now click on the &lt;strong&gt;Header&lt;/strong&gt; tab and check the status that should be &lt;strong&gt;204 No Content&lt;/strong&gt; which means the put request successfully updated the BookItem resource.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fj63tyrxvf1zgduruzgjj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fj63tyrxvf1zgduruzgjj.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Delete Web Resource/Data Operation
&lt;/h3&gt;

&lt;p&gt;Now we want to delete second BookItem resource that we created earlier.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select &lt;strong&gt;DELETE&lt;/strong&gt; HTTP verb from the drop down.&lt;/li&gt;
&lt;li&gt;Enter request URL &lt;strong&gt;&lt;a href="https://localhost:5001/api/BookItems/2" rel="noopener noreferrer"&gt;https://localhost:5001/api/BookItems/2&lt;/a&gt;&lt;/strong&gt; for delete request to delete second BookItem resource (id=2). &lt;/li&gt;
&lt;li&gt;Now click on &lt;strong&gt;Send&lt;/strong&gt; button.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F51fkery4u6vkkn1e4obk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F51fkery4u6vkkn1e4obk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now click on the &lt;strong&gt;Header&lt;/strong&gt; tab and check the status that should be &lt;strong&gt;200 Ok&lt;/strong&gt; which means the delete request successfully deleted the BookItem resource.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F42zfo2n8rfl33pa9cme7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F42zfo2n8rfl33pa9cme7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope this article would have given you some idea about how you can create your own ASP.Net Core 3 Web API.&lt;/p&gt;

&lt;p&gt;You can get the complete source code of BookStoreApi from my GitHub repository &lt;a href="https://github.com/sanjaysaini2000/aspnet-core3-webapi" rel="noopener noreferrer"&gt;aspnet-core3-webapi&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>webdev</category>
      <category>aspnetcore</category>
      <category>vscode</category>
    </item>
    <item>
      <title>How to deploy Angular app on GitHub Pages?</title>
      <dc:creator>Sanjay Saini</dc:creator>
      <pubDate>Sun, 22 Dec 2019 14:30:29 +0000</pubDate>
      <link>https://forem.com/sanjaysaini2000/how-to-deploy-angular-app-on-github-pages-1i41</link>
      <guid>https://forem.com/sanjaysaini2000/how-to-deploy-angular-app-on-github-pages-1i41</guid>
      <description>&lt;p&gt;In this article I am going to share with you my learning while I was trying to publish my Angular app on the GitHub Pages. I found GitHub Pages very effective yet very easy platform to publish your websites. So in this article I am explaining the process the same way I published my app. &lt;br&gt;
I have developed this simple Todo App in Angular which I am going to explain how to deploy on GitHub Pages in these simple steps...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fsj4lt9x5phdo94f5zveb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fsj4lt9x5phdo94f5zveb.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploy your local repository on GitHub
&lt;/h2&gt;

&lt;p&gt;GitHub Pages (&lt;a href="https://pages.github.com/" rel="noopener noreferrer"&gt;https://pages.github.com/&lt;/a&gt;) provides very convenient way to deploy your static websites from your GitHub repository therefore you need to have an account on the GitHub.com in order to follow this article.&lt;/p&gt;

&lt;p&gt;First you need to put your code in local repository into the remote repository at github.com so if you don’t have GitHub account you need to create it first now.&lt;/p&gt;

&lt;p&gt;Now login to your GitHub account and create repository in which you will upload your local code. Here I created repository named &lt;strong&gt;todo-app&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fi56ak5i7s080e7d703ki.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fi56ak5i7s080e7d703ki.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note down the URL of newly created repository that will be used to setup remote repository for your local repository in order to push your local repository code onto GitHub repository.&lt;/p&gt;

&lt;p&gt;Assuming you have git installed on your machine and you have already committed your code in the master branch of your local repository.&lt;/p&gt;

&lt;p&gt;Open the git bash on the app folder and run below commands with your GitHub repository URL to upload your code on github.com repository.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fomok8z1nbhrf2qi7sgwz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fomok8z1nbhrf2qi7sgwz.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ git remote add origin &amp;lt;your GitHub repository URL&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$ git push -u origin master &lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwt891b1mjqj8pw9dd94x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwt891b1mjqj8pw9dd94x.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Refresh your GitHub repository and check your code is pushed into newly created  GitHub repository.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fje3p0h8r7pgev384vdin.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fje3p0h8r7pgev384vdin.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Build your code to generate deployables
&lt;/h2&gt;

&lt;p&gt;Now we need to build our code in production mode in order to create distributable files that will be deployed on GitHub Pages. By default this deployable code generated in the &lt;strong&gt;/dist/&lt;/strong&gt; folder under the app folder but we need to generate this in the &lt;strong&gt;docs&lt;/strong&gt; folder under the app folder.&lt;/p&gt;

&lt;p&gt;So we need to make a small change in the &lt;strong&gt;angular.json&lt;/strong&gt; file and change the &lt;strong&gt;outputpath&lt;/strong&gt; value to &lt;strong&gt;docs/&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fp3ck73s1g58h4qgw9v4c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fp3ck73s1g58h4qgw9v4c.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another point to be noted is the URL of your going to be hosted app on the Github Pages would be &lt;strong&gt;&lt;a href="https://username.github.io/respositoryname" rel="noopener noreferrer"&gt;https://username.github.io/respositoryname&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Like in my case it would be &lt;em&gt;&lt;a href="https://sanjaysaini2000.github.io/todo-app" rel="noopener noreferrer"&gt;https://sanjaysaini2000.github.io/todo-app&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This URL is needed to set the base URL of our website while generating the distributable files to deploy on GitHub Pages.&lt;/p&gt;

&lt;p&gt;So now run following command with your website URL in the &lt;strong&gt;base-href&lt;/strong&gt; option  in the git bash window to generate distributable files in the &lt;strong&gt;docs&lt;/strong&gt; folder.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ng build - -prod –base-href=” https://username.github.io/respository-name/” &lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;username and repository-name will be your github username and repository name. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fpcxdr6mwhsysoo14w2rw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fpcxdr6mwhsysoo14w2rw.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go to your app folder and check &lt;strong&gt;docs&lt;/strong&gt; folder is created that contains all the distributable files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F66qsn8w78pqfx9mlvgne.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F66qsn8w78pqfx9mlvgne.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now commit this folder into local repository and also push it into the GitHub repository as well by running following commands in the git bash window.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ git add .&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$ git commit -m "generated deployables"&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$ git push -u origin master &lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fggew1nviuswlwqlcslfp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fggew1nviuswlwqlcslfp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure GitHub repository for Publishing on GitHub Pages
&lt;/h2&gt;

&lt;p&gt;Once docs folder is pushed into you GitHub repository you need to open the repository settings and go to the GitHub Pages section and select &lt;strong&gt;master branch/docs folder&lt;/strong&gt; from Source drop-down and that's it....&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fb74hx8b576ezs0jtal38.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fb74hx8b576ezs0jtal38.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now open the GitHub Pages URL of your deployed app in the browser window to check successful deployment of your app on the GitHub Pages.&lt;/p&gt;

&lt;p&gt;Here's the &lt;a href="https://sanjaysaini2000.github.io/angular-todo-app/" rel="noopener noreferrer"&gt;Link&lt;/a&gt; of this todo-app on GitHub Pages.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I had to create another repository name angular-todo-app so dont get confused with different repository name in the URL of this site.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fi3hd9cpk3g8roazdovtv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fi3hd9cpk3g8roazdovtv.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
There is  another method to deploy you Angular app only on GitHub Pages using angular-cli-ghpages package that you can google for more details but personally I find this approach better and clear than using that packages.&lt;/p&gt;

&lt;p&gt;Happy Reading...!!!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>github</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Create CRUD App Using Entity Framework Core 3.0 with SQL Server</title>
      <dc:creator>Sanjay Saini</dc:creator>
      <pubDate>Fri, 20 Dec 2019 06:40:09 +0000</pubDate>
      <link>https://forem.com/sanjaysaini2000/create-crud-app-using-entity-framework-core-3-0-with-sql-server-4l1j</link>
      <guid>https://forem.com/sanjaysaini2000/create-crud-app-using-entity-framework-core-3-0-with-sql-server-4l1j</guid>
      <description>&lt;p&gt;In this article I will be sharing how we can simple CRUD app using Entity Framework Core edition which is basically an open source and cross-platform version of the Entity Framework to be used in the .Net Core application development.&lt;/p&gt;

&lt;p&gt;With EF Core, data access is performed using a model. A model is made up of entity classes and a context object (derived from DbContext class) that represents a session with the database, allowing you to query and save data in the database.&lt;/p&gt;

&lt;p&gt;Here I will be using code first approach without existing database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;In order to develop and run .Net Core app, we need &lt;strong&gt;.Net Core SDK&lt;/strong&gt; that includes .Net CLI tools, .Net Core Runtime.&lt;br&gt;
So download and install the latest version of .Net Core SDK available from this &lt;a href="https://dotnet.microsoft.com/download" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;br&gt;
We will also need code editor, I will recommend VS Code but if you already have any other code editor that you are using to write C# code then stick with that otherwise download and install VS Code from this &lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;br&gt;
After installing SDK and VS Code, open the command prompt and run following command to check if SDK installed properly.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet --version&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
You must get the latest version number as a result. In my case its 3.0.101 since I had installed it sometime back.&lt;br&gt;
After done with the environment setup we are ready to develop our CRUD console app.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create a new project
&lt;/h2&gt;

&lt;p&gt;Now go to the folder location where you want to create app project and open the command window. &lt;br&gt;
Create console type project for our CRUD app by running following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet new console -o EFCore-CrudApp&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will create &lt;em&gt;EFCore-CrudApp&lt;/em&gt; folder with the same name project file in it. &lt;br&gt;
Now we need to work under this folder so move into this folder. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ cd EFCore-CrudApp&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbhrkkoh02y45h0tdnsra.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbhrkkoh02y45h0tdnsra.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Add Package References
&lt;/h2&gt;

&lt;p&gt;We need to install Entity Framework Core and for that we need to install the package for the EF Core database provider that we want to target. In this app we will be using SQL Server so run following command to install the Entity Framework Core.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 3.0.0&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I have used specific versions compatible with .net core SDK 3.0 but if you have latest version then you should run these command without specifying the version.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You find following entry in the app project file after successful installation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;ItemGroup&amp;gt;&lt;br&gt;
    &amp;lt;PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" /&amp;gt;&lt;br&gt;
  &amp;lt;/ItemGroup&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As we are done with our app project setup, now open VS code in the project folder to write code for this app.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create the model
&lt;/h2&gt;

&lt;p&gt;Our model will consists 2 POCO classes and a context class.&lt;br&gt;
Let’s create &lt;em&gt;Model.cs&lt;/em&gt; and create following classes to represent Member and Book entities in our Model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public class Member
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string PhoneNo { get; set; }
        public string City { get; set; }
    }
    public class Book
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Gener { get; set; }   
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also create &lt;em&gt;CrudAppDbContext&lt;/em&gt; class derived from DbContext that represents session with the database. This class object will be used to perform crud operations on the database and its properties will represent table in the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class CrudAppDbContext: DbContext
{
    public DbSet&amp;lt;Member&amp;gt; Members { get; set; }
    public DbSet&amp;lt;Book&amp;gt; Books { get; set; }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also need to configure the database connection that will be used by the EF Migrations to sync that database with the data model in the application.&lt;br&gt;
So add following code in the &lt;em&gt;CrudAppDbContext&lt;/em&gt; class to pass the database connection while configuring the database context for this app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;protected override void OnConfiguring(DbContextOptionsBuilder options)
{
   options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFCoreCrudAppDb;Trusted_Connection=True;");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Entity Framework Migrations
&lt;/h3&gt;

&lt;p&gt;Entity Framework provides a way to create a database from the data model using Migrations. Migrations are used to keen database in sync with the changes in the application’s data model by updating the database schema while preserving existing data in the database.&lt;/p&gt;

&lt;p&gt;So in order to create migrations we need to install Entity Framework Core tools that includes “dotnet ef” tool and the "EF designer".&lt;/p&gt;

&lt;p&gt;Run following command in the command window to install the EF Core tools.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet tool install --global dotnet-ef –version 3.0.0&lt;br&gt;
$ dotnet add package Microsoft.EntityFrameworkCore.Design –version 3.0.0&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I have used specific versions compatible with .net core SDK 3.0 but if you have latest version then you should run these command without specifying the version.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0"&amp;gt;&lt;br&gt;
      &amp;lt;IncludeAssets&amp;gt;runtime; build; native; contentfiles; analyzers; buildtransitive&amp;lt;/IncludeAssets&amp;gt;&lt;br&gt;
      &amp;lt;PrivateAssets&amp;gt;all&amp;lt;/PrivateAssets&amp;gt;&lt;br&gt;
 &amp;lt;/PackageReference&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Create the database
&lt;/h2&gt;

&lt;p&gt;Since we have installed Entity Framework Core tools, we are ready to create database from our application data model.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, we need to add migrations that will create the database schema of our model entities. 
So run following commands to create the database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ dotnet ef migrations add InitialCreate&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fp8nnol7liynshz37yswi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fp8nnol7liynshz37yswi.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It will add Migrations folder in the project with auto-generated files for “initialCreate” migration.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Second, we need to apply “initialCreate” migration on the database. Since it’s a very first migration and we don’t have existing database so it will create new database for us.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So run following command to apply the migration to the database.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet ef database update&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fqchyugdxnkydpbt9pd8e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fqchyugdxnkydpbt9pd8e.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A new database is created along with the tables for the model entities.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Add CRUD Operations Code
&lt;/h2&gt;

&lt;p&gt;Since the database is created, we are ready to add code for crud operations.&lt;/p&gt;

&lt;p&gt;Add new file &lt;em&gt;CrudUtility.cs&lt;/em&gt; in the project to add crud operations code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add following code to create (insert) data in the Books table in database.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        public static void AddBook()
        {
            using (var dbcontext = new CrudAppDbContext())
            {
                Console.WriteLine("**********Enter Book details**********");
                Console.WriteLine("--------------------------------------");
                Console.Write("Book Title : ");
                string title = Console.ReadLine();
                Console.Write("Book Gener : ");
                string gener = Console.ReadLine();
                Console.WriteLine(" ");

                var book = new Book { Title = title, Gener = gener };
                dbcontext.Books.Add(book);
                dbcontext.SaveChanges();
            }
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Add following code to read data from the Books table.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        public static void GetBooks()
        {
            using (var dbcontext = new CrudAppDbContext())
            {
                var query = from b in dbcontext.Books orderby b.Id select b;
                Console.WriteLine("**********List of Books**********");
                Console.WriteLine("---------------------------------");
                foreach (var b in query)
                {
                    Console.WriteLine("Details of Book No - {0}", b.Id);
                    Console.WriteLine("--------------------------------- ");
                    Console.WriteLine("Book Title - {0}", b.Title);
                    Console.WriteLine("Book Gener - {0}", b.Gener);
                    Console.WriteLine(" ");
                }
            }
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Add following code to update a particular Book data in the Books table.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     Public static void UpdateBookDetails(int bookId, string title, string gener)
        {
            using (var dbcontext = new CrudAppDbContext())
            {
                var query = from b in dbcontext.Books orderby b.Id select b;
                foreach (var b in query)
                {
                    if (b.Id == bookId)
                    {
                        b.Title = title;
                        b.Gener = gener;
                        dbcontext.Update(b);
                        break;
                    }
                }
                dbcontext.SaveChanges();
            }
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Add following code to delete a particular Book data from the Books table.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        public static void DeleteBook(int bookId)
        {
            using (var dbcontext = new CrudAppDbContext())
            {
                var query = from b in dbcontext.Books orderby b.Id select b;
                foreach (var b in query)
                {
                    if (b.Id == bookId)
                    {
                        dbcontext.Remove(b);
                        break;
                    }
                }
                dbcontext.SaveChanges();
            }
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Same way add code to perform CRUD operations on “Member” entity as well.&lt;/p&gt;

&lt;p&gt;Now we have to call these CRUD operations in our app so add following code in the Main method of Program.cs file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            CrudUtility.AddBook();
            CrudUtility.GetBooks();

            Console.Write("Enter Book's Id to be deleted : ");
            int deletedBookId = int.Parse(Console.ReadLine());
            CrudUtility.DeleteBook(deletedBookId);

            Console.Write("Enter Book's Id to be updated : ");
            int updatedBookId = int.Parse(Console.ReadLine());
            Console.Write("Enter Book's Title : ");
            string bookTitle = Console.ReadLine();
            Console.Write("Enter Book's Gener : ");
            string bookGener = Console.ReadLine();
            CrudUtility.UpdateBookDetails(updatedBookId, bookTitle, bookGener);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can get the complete source code from my GitHub repository &lt;a href="https://github.com/sanjaysaini2000/EFCore-CrudApp" rel="noopener noreferrer"&gt;EFCore-CrudApp&lt;/a&gt; with the details about how to setup the code on your machine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run the App
&lt;/h2&gt;

&lt;p&gt;At last run the app with following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ dotnet run&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Happy reading...&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>entityframework</category>
      <category>csharp</category>
      <category>vscode</category>
    </item>
    <item>
      <title>Deploy Your Angular App on Firebase.</title>
      <dc:creator>Sanjay Saini</dc:creator>
      <pubDate>Tue, 17 Dec 2019 08:32:11 +0000</pubDate>
      <link>https://forem.com/sanjaysaini2000/deploy-your-angular-app-on-firebase-94g</link>
      <guid>https://forem.com/sanjaysaini2000/deploy-your-angular-app-on-firebase-94g</guid>
      <description>&lt;p&gt;Here I am going to share with you how you can deploy your Angular app on Firebase.&lt;br&gt;
In case you don’t know what Firebase is, it’s a mobile and web application development and deployment platform. For more details checkout this &lt;a href="https://firebase.google.com/"&gt;link&lt;/a&gt;.&lt;br&gt;
I have developed this simple frontend only (No backend) Todo App in Angular which I am going to use to explain how simple it is to deploy your App on Firebase.&lt;br&gt;
If you already have any App ready for deployment you can follow along with me as I am explaining these simple steps in this article.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6IRXqkPz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/llh5wtgn8r5yid0avkbr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6IRXqkPz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/llh5wtgn8r5yid0avkbr.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are interested in the source code of this App, you can get it from link of my &lt;a href="https://github.com/sanjaysaini2000/angular-todo-app/tree/firebase"&gt;angular-todo-app&lt;/a&gt; GitHub repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create Firebase Account
&lt;/h2&gt;

&lt;p&gt;In order to deploy your app on Firebase you must first need to have an account on the firebase website (&lt;a href="https://console.firebase.google.com"&gt;https://console.firebase.google.com&lt;/a&gt;). The good news is that Firebase expects google account for login and I am sure you already have one otherwise you can create one if you don’t have or you can create another google account for Firebase if you don’t want to use existing one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create New Firebase Project
&lt;/h2&gt;

&lt;p&gt;Once you are done with the account creation, now create your first Firebase project by clicking on the Add Project button. It’s a 3 steps process.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, provide unique name to your project. In my case I have named it &lt;em&gt;angular7-todo-app&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Second, enable google analytics for this project, which is by default enabled so click on continue.&lt;/li&gt;
&lt;li&gt;Finally, configure the google analytics by selecting &lt;em&gt;“default account for firebase”&lt;/em&gt; from the dropdown field and click on create project.
It will take 1-2 minutes to create the project and that’s all you need to do on Firebase site for now in order to deploy this Todo app. Although you can do much more with Firebase but that’s not in scope for this article.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aW3pc8Qa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/1eluay7bvfl338b9cl48.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aW3pc8Qa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/1eluay7bvfl338b9cl48.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have all the setup needed at the Firebase site done, we are few more steps away from the deployment of our app on Firebase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploy App with Firebase Tool
&lt;/h2&gt;

&lt;p&gt;We will require Firebase tools installed on the machine for the deployment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Firebase Tool
&lt;/h3&gt;

&lt;p&gt;So open the command window and navigate to your app project folder and install firebase-tools on your machine via npm by running following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ npm install –g firebase-tools&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Next we need to login to Firebase site from the console using following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ firebase login --interactive&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
This will open the Firebase login in the browser for you to login.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rCcLkY62--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/la93ravl50ep81skib2m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rCcLkY62--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/la93ravl50ep81skib2m.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Initialize Firebase Project
&lt;/h3&gt;

&lt;p&gt;Next we need to initialize Firebase project from our project folder using following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ firebase init&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
This will start a selection wizard to initialize Firebase project with following steps in the console.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step-1. Select “Y” to proceed and then select Hosting option since we want to only host our app at this moment.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GC20nV1G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/2xi43zaxx01zht87cnpp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GC20nV1G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/2xi43zaxx01zht87cnpp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step-2. Now select existing project option since we already created Firebase project for this app hosting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wFcNK-YO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ta77sg0icfbliqj1a5bq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wFcNK-YO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ta77sg0icfbliqj1a5bq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step-3. So now select the newly created Firebase project which in my case is &lt;em&gt;angular7-todo-app&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uPpYfsOQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/5xtg9uowrc8xkad06h65.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uPpYfsOQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/5xtg9uowrc8xkad06h65.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step-4. Select “No” to configure the app as single page app since we will configure it by updating &lt;em&gt;firebase.json&lt;/em&gt; file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yb53cy3U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/vx2xx5562v6m880yd62t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yb53cy3U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/vx2xx5562v6m880yd62t.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the end, Firebase initialization wizard will create following files in the project folder.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;404.html&lt;br&gt;
Index.html&lt;br&gt;
.firebaserc&lt;br&gt;
Firebase.json&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Delete this index.html file since you might already have index.html in your app project folder.&lt;br&gt;
Now edit this newly created firebase.json file and add following configuration.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"public": "docs",&lt;br&gt;
    "rewrites": [&lt;br&gt;
      {&lt;br&gt;
        "source": "**",&lt;br&gt;
        "destination": "/index.html"&lt;br&gt;
      }&lt;br&gt;
    ],&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xfkYGRIj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/75o5pdunz6x7saowu80x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xfkYGRIj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/75o5pdunz6x7saowu80x.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note – make sure the outputPath value in the angular.json should be &lt;strong&gt;“docs”&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6rfTrzxF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/e8749ua9n2ti9dez3w5i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6rfTrzxF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/e8749ua9n2ti9dez3w5i.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Build App Project
&lt;/h3&gt;

&lt;p&gt;Since we are done with initialization of our Firebase project, now we need to create production release build for our app. &lt;br&gt;
So run the following command on the command window to generate build files in the “docs” folder.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ng build - -prod &lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Deploy the App
&lt;/h3&gt;

&lt;p&gt;With all the setup is done and release build is ready, we are one step away to deploy our app on the Firebase site.&lt;br&gt;
Now run following command to deploy the app.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ firebase deploy&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0RrU8QMp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/iciigdvsjd8g97pb4b4w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0RrU8QMp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/iciigdvsjd8g97pb4b4w.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;...and we are done!!!&lt;/p&gt;

&lt;p&gt;Now open the hosting URL &lt;a href="https://angular7-todo-app.firebaseapp.com"&gt;https://angular7-todo-app.firebaseapp.com&lt;/a&gt; in the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9nKmcmXQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ykqym2fedim2qc62v5l0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9nKmcmXQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ykqym2fedim2qc62v5l0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>firebase</category>
      <category>deployment</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Create Multi-Project App in .Net Core 3.0</title>
      <dc:creator>Sanjay Saini</dc:creator>
      <pubDate>Mon, 16 Dec 2019 08:57:35 +0000</pubDate>
      <link>https://forem.com/sanjaysaini2000/create-multi-project-app-in-net-core-3-0-3e41</link>
      <guid>https://forem.com/sanjaysaini2000/create-multi-project-app-in-net-core-3-0-3e41</guid>
      <description>&lt;p&gt;Some time back .Net Core version 3.0 was released and I thought to explore it and then an idea came to me about creating multi project application the same way we create in .Net Framework.&lt;br&gt;
.Net Core is an open source framework for developing and deploying cross platform application on any platform. Check out this &lt;a href="https://docs.microsoft.com/en-us/dotnet/core/about"&gt;link&lt;/a&gt; to learn more about .Net Core.&lt;br&gt;
To demonstrate how we can create, build and deploy multi project app in the .Net Core 3.0, I have decided to develop a sample functional CVS Validator console app. I will explain the functionality of this app later.&lt;br&gt;
First, we need to setup development environment.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;p&gt;We basically need .Net CLI tools, .Net Core Runtime and a code editor in order to develop and run .Net Core app on our machine.&lt;br&gt;
.Net CLI tools, .Net Core Runtime comes with the .Net Core SDK so download and install the latest version of .Net Core SDK available from this &lt;a href="https://dotnet.microsoft.com/download"&gt;link&lt;/a&gt;.&lt;br&gt;
And for code editor I will recommend VS Code but if you already have any other code editor that you are using to write C# code then stick with that otherwise download and install VS Code from this &lt;a href="https://code.visualstudio.com/download"&gt;link&lt;/a&gt;.&lt;br&gt;
After installing SDK and VS Code, open the command prompt and run following command to check if SDK installed properly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet --version
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;You must get the latest version number as a result.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After done with the environment setup we are ready to develop our CSV Validator console app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create Application Code Scaffolding
&lt;/h2&gt;

&lt;p&gt;This app will have a solution file and three projects to contain code for client, CSV data validation functionality and unit testing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create Solution and Projects
&lt;/h3&gt;

&lt;p&gt;So now go to the folder location where you want to develop the app and open the command window (since I have GIT installed on my machine, I will be using git bash window).&lt;br&gt;
Create solution by running following command in the command window.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet new sln -o CsvValidatorApp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It will create CsvValidatorApp folder with the same name solution file in it. &lt;br&gt;
Now we need to create projects under this folder so move into this folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd CsvValidatorApp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;First, we will create console type project that will be startup Client project and entry point of our app. So run following command in the command window to create it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet new console -o CsvValidator.Client
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Second one will be class library type project that will contain all the functionality for loading, validating and rendering validated csv file. So run following commands in the command window to create it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet new classlib -o CsvValidator.Data
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The last one will be the nunit type project for the unit testing of class library project code that can be created by running following commands in the command window&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet new nunit -o CsvValidator.Test
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Add Projects in the Solution
&lt;/h3&gt;

&lt;p&gt;Now we need to include the projects into the solution by running following command in the command window.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet sln CsvValidatorApp.sln add CsvValidator.Client/CsvValidator.Client.csproj CsvValidator.Data/CsvValidator.Data.csproj CsvValidator.Test/CsvValidator.Test.csproj

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Add Project Reference
&lt;/h3&gt;

&lt;p&gt;Now we need to add reference of our class library Data project in the console app Client project. For that run following command in the command window.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet add CsvValidator.Client/CsvValidator.Client.csproj reference CsvValidator.Data/CsvValidator.Data.csproj
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And also add reference of class library Data project in the Test project by running following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet add CsvValidator.Test/CsvValidator.Test.csproj reference CsvValidator.Data/CsvValidator.Data.csproj
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Build and Run Application
&lt;/h3&gt;

&lt;p&gt;As we are done with all the scaffolding code creation we are now ready to build the app for the first time by running following command in the command window.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet build CsvValidatorApp.sln
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I am sure you won’t get any error since there is not much code in there.&lt;br&gt;
You can run the app by running the console Client project and get the standard “Hello World” message in the command window. &lt;br&gt;
Run following command to run the app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet run -p CsvValidator.Client/CsvValidator.Client.csproj
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;or change to CsvValidator.Client folder and run following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet run
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And To run the test project, simple run following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dotnet test CsvValidator.Test/CsvValidator.Test.csproj
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Start Coding the App…
&lt;/h3&gt;

&lt;p&gt;Now you are ready to put flash into bare bone scaffolding code of this app and for that we need to open the code editor in the application solution folder. &lt;br&gt;
To open the VS Code in the folder, run following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;code .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Get the source code of this app from my GitHub repository from this &lt;a href="https://github.com/sanjaysaini2000/netcore3-csvvalidator"&gt;link&lt;/a&gt; and start adding code files and code details in your app.&lt;/p&gt;

&lt;h3&gt;
  
  
  About CVS Validator App
&lt;/h3&gt;

&lt;p&gt;This app take the csv file and validate the data according to the validation provided to it in an xml file and generate the validated csv file at the location of the source csv file. Both files path are configured in the app configuration file. The sample of both files are in my GitHub repository along with the source code of the app. More details about the app can be found in the README.md file in the repository as well.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>dotnetcore</category>
      <category>vscode</category>
    </item>
  </channel>
</rss>
