NodeJs for beginners: 03 – Creating Server
In this blog we will learn how we can create a server and listen for requests. Node js and NPM should be installed in your system. If you haven’t installed then you can check my previous blog on node js over here : NodeJs for beginners: 02 – How to install node js ?
Open VS code or any code editor of your choice and open a terminal and type following command:
npm init -y. This command will create a new file in your directory named as
package.json. This file is used to manage node packages. You’ll know more about this file as soon as you compelete this node series. Now make a new file and name it as
index.js or whaterver name you want to give to this file.
package.json file and change following:
Image 1. package.json file
Make sure to chagne index.js to your file name. This command will basically start our server. But wait we haven’t created one. So, let’s go and make our server.
Open file index.js or whaterver file you have created. Inside that file type following codes:
Image 2. creating server in index.js file
So basically we have imported
http package which is a default package that comes with node js at the time of installation.
http package will help us to create a server.
http.createServer() is a function to create server and in line no. 8 our created server is litening to port
8000. It means that our server will send responses to all the request that are coming to port
8000. We have given a callback function to
listen() function. We’ll know about them in later but for now only undrestand that it will execute only after previous task is completed. In our case until server starts listening to port
8000, callback function wont excecute.
Now open terminal and run npm start command. You will see
server is running at port 8000 in the terminal if you are following properly. It means our server is running properly. But our server is not listening to any requests. So let’s make our server to listen to some requrests and send responses to those request. Write down following codes in index.js file:
Image 3. Listen to requests
As you can see at line 7 our server is listening to path
/ and a call back funtion is assigned to it. Callback function will execute as soon as any request comes at
/ path. The callback function is having two parameters
res which is short form of
response. These two are provided by node js itself.
req is an object which will contain details to upcoming
res is use for sending responses to upcoming
request. Now if you will open you browser and if you go to link
http://localhost:8000/, you will get a following response:
Image 4. Response to our request
What happend is basically, as soon as you open your browser and searched
http://localhost:8000/, your browser sent a req to
localhost:8000. Here localhost means you pc and
8000 is port number. As our server is running at port
8000 and litening to
/ path, we got response in the browser. This is how nodejs handles each request. This is it for this blog. Check out my next blog :