16. Creating Express Server

  1. Create a separate folder in your vs code named as “devTinder“ in that folder executed a command “npm init“ to initialize a folder when you write this command in terminal it will ask different configurations like folder name, test command, author etc., basically it is like a index of project which is like a configuration file of your project. If you wan to skip all this configuration simply write “npm init -y“ . As soon as this command is executed a file named as “package.json“ is created inside dev-tinder folder this file contain basic project dependencies and configuration. When this file is created you can fill all the configuration manually whenever needed.

    this image is generated simply by writing “npm init -y“ in terminal

  2. Create a “src“ folder inside devTinder in that src folder create a file “app.js“ which is used to hold code for writing backend application. In this file we write all code.

  3. Now in terminal inside the devTinder folder write this command “npm i express“ this will install the express package inside your application, express is used to create server for node js application as soon as this command executed this will create a “node_modules“ folder which is used to store code of packages from internet they are heavy coded files and folder and avoid them while pushing to version control system like GitHub by including them inside the “.gitignore“ files, they can be regenerated when deleted or not found simply by writing “npm i“ sometime we download the repository from github and simply open them inside vs code and in terminal write this command and all node_modules are generated again and a file named as “package-lock.json“ is also created which has primary role is to lock down the exact version of every package and its dependencies that are installed in your project.

  4. Write following command inside the app.js file inside src

     1.const express = require("express");
     2.const app = express();
     3.const Port = 3000;
     4.app.use("/test", (req, res) => {
     5.  res.send("hello from test");
     6.});
     7.app.listen(Port, () => {
     8.  console.log(`Listening on port ${Port}`);
     9.});
    

    Lets see how above code work:-

    1st line is used to acquire express package from node_modules inside our code to create node application or we can say to to create a instance of express,2nd line is used to create a application using express, 3rd line is telling port at which our application run locally (http://localhost:3000) in browser, 4th line to 6th line is used to define a handler or middleware that execute on every request regardless of the HTTP methods(GET, POST, PATCH, DELETE) only for specific URL, req and res are request and response respectively which we write in callback to send required message on the screen in browser , 7th to 9th line is used to allow your application to host on specific port using listen method.

  5. Now navigate to your devTinder folder and run command “node ./src/app.js“ and in vs code terminal this code will be seen “listening on port 3000“ and in browser white screen “hello from test“ is seen

  6. When any changes are made inside the “app.js“ file, every time we have to executed the command “node ./src/app.js“ to see changes, to avoid restarting of server again and again we install a package known as “nodemon“ to install this simply write this command “npm i nodemon -g(if want to install globally)“ after this you can either run “npx nodemon .\src\app.js“ and execute or you can add script inside package.json fille like this:-

    now you can run command “npm run start“ to open your “app.js“ file using nodemon.

  7. Inside package-lock.json

    we can see two symbols inside dependencies like this ^(caret) and ~(tilda) what are these

    ^ is to update the packages both for minor and patch version whereas ~ is for only patch

Notes:- To get well-versed with above things try these below task at your end.

 - Create a repository
 - Initialize the repository
 - node_modules, package.json, package-lock.json
 - Install express 
 - Create a server
 - Listen to port 7777
 - Write request handlers for /test , /hello
 - Install nodemon and update scripts inside package.json
 - What are dependencies
 - What is the use of "-g" while npm install 
 - Difference between caret and tilde  ( ^ vs ~ )