You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
1.9 KiB
JavaScript
32 lines
1.9 KiB
JavaScript
// Use the "require" directive to load node.js modules
|
|
// A module is a software component or part of a program that contains one or more routines
|
|
// The "http module" it lets Node.js transfer data using the Hyper Text Transfer Protocol
|
|
// The "http module" is a set of individual files that contain code to create a "component" that helps establish data transfer between applications.
|
|
// HTTP is a protocol that allows the fetching of resources suchh as HTML documents
|
|
// Clients (browser) and servers (nodeJS/express JS applications) communicate by exchanging individual messages.
|
|
// The messages sent by the "client", usually a web broswer, are called "request"
|
|
// The messages sent by the "server" as an answer are called "responses"
|
|
let http = require("http");
|
|
|
|
|
|
// Using this module's createServer() method, we can create an HTTP server that listens to requests on a specified port and gives responses back to the client
|
|
// The http module has a createServer() method that accepts a function as an argument and allows for a creation of a server
|
|
// The arguments passed in the function are request and response object (data type) that contains methods that allow us to receive requests from the client and send responses back to it
|
|
// A port is a virtual point where network connections start and end.
|
|
// Each port is associated with a specific process or service
|
|
// The server will be assigned to port 4000 via the "listen(4000)" method where communicating with our server happens.
|
|
|
|
http.createServer(function (request, response) {
|
|
|
|
// Use writeHead() method to:
|
|
// Set a status code for the response - a 200 means OK
|
|
// Set the content-type of the response as a plain text message
|
|
response.writeHead(200, {'Content-Type': 'text/plain'});
|
|
|
|
// Send the response with text content 'Hello World'
|
|
response.end('Hello World');
|
|
|
|
}).listen(4000)
|
|
|
|
// When server is running, console will
|
|
console.log('Server running at localhost:4000'); |