DEV Community

Cover image for Day 12 – Creating own server - Learning Node JS In 30 Days [Mini series]
Muhammad Ali (Nerdjfpb)
Muhammad Ali (Nerdjfpb)

Posted on • Originally published at blog.nerdjfpb.com

Day 12 – Creating own server - Learning Node JS In 30 Days [Mini series]

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

Alt Text

First we are going to require http and store it to http variable using const http = require('http')

Alt Text

We are going to use http.createServer for creating the server

Alt Text

We can just start our server using const server = http.createServer() but there are some more work to do.

Alt Text

We need to write a function inside the createServer() so that we can get the request and response according about it

Alt Text

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.

Alt Text

Now run the code using node app.js

Alt Text

and finally go to localhost:3000 and you can see the result

Alt Text

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)

Top comments (0)