Faisal Julaidan Faisal Julaidan - 6 months ago 83
Node.js Question

No need to set up a local server with Node.js?

I see when I want write a Node.js web application on my local machine, I don't need to set-up a local server using WAMP or MAMP. What is node.js really doing behind the scenes? I am providing this code to make a simple hello world web app:

var http = require("http");


response.writeHead(200, {"content-type":"text/html"});
response.write("hello world");


console.log("server is running....");

When loading in my browser URL bar "localhost:8080" it works.

How this is working as and why don't I need a local server when working with Node.js?


You do have a local server... it's your Node.js application.

When you call http.createServer(), it creates an HTTP server. When you call .listen() on that server, it binds to the requested port, and optionally requested address, and listens for connections. When data comes in on those connections, it responds like any other HTTP server.

The HTTP server uses your request/response callback, firing it whenever a valid HTTP request comes in.