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.');
})
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')
});
Top comments (0)