16. Creating Express Server

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
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.
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.
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.
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
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.
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 ~ )
Subscribe to my newsletter
Read articles from Faraz Alam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Faraz Alam
Faraz Alam
Assalamualaikum warahmatullah wabarakatuh( traditional Islamic greeting in Arabic "Assalamu alaikum": "Peace be upon you." "Wa rahmatullahi": "And the mercy of Allah." "Wa barakatuh": "And His blessings.") I’m Faraz Alam, and I’m documenting my journey through the world of software technology. Despite earning a master’s degree in Computer Applications and having access to opportunities provided by my tier-3 college, I struggled to take full advantage of them due to poor management and a less productive environment. This led to joblessness, primarily due to a lack of upskilling. Now, I am dedicated to enhancing my skills and knowledge with the aim of securing a valuable job offer from leading product-based companies, including those in the FAANG group (Facebook, Amazon, Apple, Netflix, Google) and other prominent tech giants. This documentation is not for self-promotion; rather, it is for anyone who is waiting for an opportunity but feels they lack the tools and skills required to overcome challenges. It’s a testament to the effort and responsibility needed to navigate the journey towards success when you take charge of your own path. Date: 31 July 2024, 07:25 AM This page will be updated regularly to reflect new achievements and milestones as I continue to build my career.