Advertisement
karlakmkj

Node.js - http module

Oct 12th, 2021
1,260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // http module : leveraging Node.js networking and creating HTTP servers and processing HTTP requests.
  2.  
  3. // Import the http module
  4. const http = require('http');
  5.  
  6. // Create a simple server
  7. const server = http.createServer((req, res) => {  //callback function with request & response as the arguments
  8.   res.end('Hello World');
  9. });
  10.  
  11. /*
  12. The req object contains all of the information about an HTTP request ingested by the server. It exposes information such as the HTTP method (GET, POST, etc.), the pathname, headers, body, and so on. The res object contains methods and properties pertaining to the generation of a response by the HTTP server. This object contains methods such as .setHeader() (sets HTTP headers on the response), .statusCode (set the status code of the response), and .end() (dispatches the response to the client who made the request).
  13. */
  14.  
  15. // Once the .createServer() method has instantiated the server, it must begin listening for connections.
  16. server.listen(4001,() => {
  17. const { address, port } = server.address();
  18.   console.log(`Server is listening on: http://${address}:${port}`);
  19. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement