DEV Community

Rittwick Bhabak
Rittwick Bhabak

Posted on • Updated on

Node.js Crash Course - Part 3 - Clients & Servers

Creating a server in node

const http = require('http');

const server = http.createServer((req, res)=>{
    console.log('Everytime a request is made, I pops up.');
})

Enter fullscreen mode Exit fullscreen mode

The callback function in the above code block runs every time a request is made.
The arguments of the callback function are req and res. In req, the information about incoming request is present and through req the response is sent back.

This alone is not doing anything. The server have to actively listen to the requests.
server.listen()

server.listen(3000, 'localhost', ()=>{
    console.log('Listening to port number 3000')
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)