Client make a request to the server and server provide the files using socket. So we are going to learn about how to make our own server using node today!
We are going to use http module
for this. You can find the official document here https://nodejs.org/dist/latest-v12.x/docs/api/http.html
First we are going to require http
and store it to http
variable using const http = require('http')
We are going to use http.createServer for creating the server
We can just start our server using const server = http.createServer()
but there are some more work to do.
We need to write a function inside the createServer()
so that we can get the request and response according about it
Now we can send data
to request using res.end('Hey everyone!')
and we need to give a port to listen. In my case I'm using 3000
.
Now run the code using node app.js
and finally go to localhost:3000 and you can see the result
So creating server is easy right ?
You can see the graphical version here
Originally it published on nerdjfpbblog. You can connect with me in twitter or linkedin !
You can read the old posts from this series (below)
![nerdjfpb](https://res.cloudinary.com/practicaldev/image/fetch/s--gdH55IFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--PuDDQYlr--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/217204/5d66fc09-f166-4a95-a5e1-94436bedbe7d.jpeg)
Day 1 — Introduction - Learning Node JS In 30 Days [Mini series]
Muhammad Ali (Nerdjfpb) ・ Dec 4 '19 ・ 1 min read
![nerdjfpb](https://res.cloudinary.com/practicaldev/image/fetch/s--gdH55IFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--PuDDQYlr--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/217204/5d66fc09-f166-4a95-a5e1-94436bedbe7d.jpeg)
Day 2 - Install and Running Node on Window - Learning Node JS In 30 Days [Mini series]
Muhammad Ali (Nerdjfpb) ・ Dec 5 '19 ・ 1 min read
![nerdjfpb](https://res.cloudinary.com/practicaldev/image/fetch/s--gdH55IFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--PuDDQYlr--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/217204/5d66fc09-f166-4a95-a5e1-94436bedbe7d.jpeg)
Day 3 - JavaScript Engine - Learning Node JS In 30 Days [Mini series]
Muhammad Ali (Nerdjfpb) ・ Dec 6 '19 ・ 2 min read
![nerdjfpb](https://res.cloudinary.com/practicaldev/image/fetch/s--gdH55IFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--PuDDQYlr--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/217204/5d66fc09-f166-4a95-a5e1-94436bedbe7d.jpeg)
Day 4 - Window === Global ? - Learning Node JS In 30 Days [Mini series]
Muhammad Ali (Nerdjfpb) ・ Dec 7 '19 ・ 2 min read
![nerdjfpb](https://res.cloudinary.com/practicaldev/image/fetch/s--gdH55IFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--PuDDQYlr--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/217204/5d66fc09-f166-4a95-a5e1-94436bedbe7d.jpeg)
Day 5 - Function Declarations vs. Function Expressions - Learning Node JS In 30 Days [Mini series]
Muhammad Ali (Nerdjfpb) ・ Dec 8 '19 ・ 1 min read
![nerdjfpb](https://res.cloudinary.com/practicaldev/image/fetch/s--gdH55IFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--PuDDQYlr--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/217204/5d66fc09-f166-4a95-a5e1-94436bedbe7d.jpeg)
Day 6 - Require & Module - Learning Node JS In 30 Days [Mini series]
Muhammad Ali (Nerdjfpb) ・ Dec 9 '19 ・ 2 min read
![nerdjfpb](https://res.cloudinary.com/practicaldev/image/fetch/s--gdH55IFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--PuDDQYlr--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/217204/5d66fc09-f166-4a95-a5e1-94436bedbe7d.jpeg)
Day 7 - More Modules - Learning Node JS In 30 Days [Mini series]
Muhammad Ali (Nerdjfpb) ・ Dec 10 '19 ・ 2 min read
![nerdjfpb](https://res.cloudinary.com/practicaldev/image/fetch/s--gdH55IFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--PuDDQYlr--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/217204/5d66fc09-f166-4a95-a5e1-94436bedbe7d.jpeg)
Day 8 – Var vs Let vs Const -Learning Node JS In 30 Days [Mini series]
Muhammad Ali (Nerdjfpb) ・ Dec 11 '19 ・ 2 min read
![nerdjfpb](https://res.cloudinary.com/practicaldev/image/fetch/s--gdH55IFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--PuDDQYlr--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/217204/5d66fc09-f166-4a95-a5e1-94436bedbe7d.jpeg)
Top comments (0)