on-internet
← Community

how does the createServer() work in node.js

Stack Overflow ·

I am currently learning Node.js and came across the http.createServer() method. I would appreciate it if someone could help me understand how this method works. In particular, I would like to understand: What exactly does http.createServer() do? What are req and res in the callback function? How does the server handle incoming requests and send responses? What is the purpose of .listen(8080)? Here is the code I am trying to understand: let http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World!'); }).listen(8080)

Comments

Sign in or sign up to comment and upvote.

No comments yet.