Skip to content

Instantly share code, notes, and snippets.

@tararoutray
Last active September 20, 2021 02:56
Show Gist options
  • Save tararoutray/c4435c1f8037a5eee8701dcf17c86cb0 to your computer and use it in GitHub Desktop.
Save tararoutray/c4435c1f8037a5eee8701dcf17c86cb0 to your computer and use it in GitHub Desktop.
// Import the HTTP module by using the require() method.
// The require() method is available globally
const http = require('http');
// Since we have imported the HTTP module,
// we can use methods from that module.
// Call the createServer() method inorder to create a server.
// This method requires a function or an anonymous function.
// And that function requires two arguments,
// one is: request - of type incoming message,
// and other is: response - of type object.
// This http.createServer() method returns a server instance.
// So, let's store this into a constant with a name "server".
const server = http.createServer((request, response) => {
// This will console.log() will only be triggered when we complete
// setting up the node server, and a request is received by this
// server.
console.log(request);
});
// From the server const, let's call a method named listen().
// Listen() actually starts a process where Node.js will not immediately exit
// our script. But instead it will keep the process running to listen for
// incoming requests.
// The listen() method takes a couple of arguments.
// The first one is: port (Port number on which you want to listen. By default,
// it takes port 80. But in our local machine we need to assign one like 3000).
// The second is: hostname (By default it will take the name of the machine
// it is running on. For our local machine, it will take localhost).
// Let's allocate a port: 3000. This tells Node.js to listen to this port.
// example: http://localhost:3000
server.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment