Skip to content

The http Module Part -1

Setup our Project

  1. Create a new folder, and name it "myserver".
  2. Initialize the npm: npm init
  3. Install the http module: npm install http
  4. Create a new file inside the "myserver" folder, and name it "index.js".
  5. Add the following lines to "index.js":
const http = require("http");

//create the server
const server = http.createServer((request, response) => {
    response.end("Hey, it's my first nodejs program");
});

server.listen(8000, () => console.log("The server is running on port 8000"));
  1. In the terminal, write nodejs index.js.
  2. You can see "The server is running on port 8000 " displayed in the terminal.
  3. Open the browser at http://localhost:8000.
  4. You can see "Hey, it's my first nodejs program" displayed in the browser.

Require Statement

Task 1: Start a new nodejs project, and name it "project1".

# Create a new folder, and name it "project1"
# Go to that "project1" directory
npm init

Task 2: Refer to Task 1; add the http module to project1.

npm install http

Task 3: Refer to Task 1; create a new file in project1, and name it index.js. Add the http module to index.js. Hint: use require('http').

const http = require('http');

require('package_name')

You can include a module/package in your file using require("module_name")

The http object

Task 4: Refer to task 3; display http in the console.

const http = require('http');
console.log(http);

// Run nodejs index.js

You get an Object:

{
  METHODS: [
    'ACL',        'BIND',        'CHECKOUT',
  ],
  STATUS_CODES: {
    '100': 'Continue',
    '101': 'Switching Protocols',
    '511': 'Network Authentication Required'
  },
  Agent: [Function: Agent] { defaultMaxSockets: Infinity },
  Server: [Function: Server],
  ServerResponse: [Function: ServerResponse],
  createServer: [Function: createServer],
  get: [Function: get],
  request: [Function: request],
}

STATUS_CODES

Task 5: Refer to task 3; since http is an object, display in the console the meaning of the status_code 200. Hint: look at the result of task 4 carefully.

const http = require('http');

console.log(http.STATUS_CODES["200"]);

The result is: Ok

Task 6: Refer to task 3; since http is an object, display in the console the meaning of the status_code 500.

const http = require('http');

console.log(http.STATUS_CODES["500"]);

The result is: 'Internal Server Error'

Task 7: Refer to task 3; since http is an object, display in the console the meaning of the status_code 404.

const http = require('http');

console.log(http.STATUS_CODES["404"]);

The result is: 'Not Found'

Task 8: Refer to task 3; since http is an object, display in the console the meaning of the status_code 400.

const http = require('http');

console.log(http.STATUS_CODES["400"]);

The result is: 'Bad Request'

HTTP Status Code Description
200 Ok
404 Not Found
400 Bad Request
500 Internal Server Error

createServer()

Task 9: Refer to task 3; call the createServer() method on the http object.

const http = require('http');

const server = http.createServer();

Task 10: Refer to task 9; call the listen(9000) method on the server object. Then, run nodejs index.js, and open the url http://localhost:9000 in the browser.

const http = require('http');

const server = http.createServer();
server.listen(9000);

createServer()

The createServer() method creates a new instance of the http object. It takes in two parameters the request and the response parameters.

server.listen(port)

The server.listen(port) method makes our server operate through the specified port in the browser.

Task 11: Add the following code to the index.js file, and run nodejs index.js. Then, go to the http://localhost:9000 in the browser.

const http = require('http');

const server = http.createServer((request, response) => {
    response.end("Hello there");
});
server.listen(9000);

You can see there: Hello there displayed in the browser.

Mini Projects

Project 1

Task 12: Create a nodejs server that listens on port "5000", and displays "I'm running on port 5000, yesss!" in the browser window.

mkdir project1 # Create a folder for the project
cd project1
npm init
npm install http
touch index.js # create index.js

Write the following in index.js:

const http = require('http');

const server = http.createServer((request, response) => {
    response.end("I'm running on port 5000, yesss!");
});
server.listen(5000);

Project 2

Task 13: Create a nodejs server that listens on port "8887", and display "Ok, I'm doing it again!" in the browser window.

mkdir project2 # Create a folder for the project
cd project2
npm init
npm install http
touch index.js # create index.js

Write the following in index.js:

const http = require('http');

const server = http.createServer((request, response) => {
    response.end("Ok, I'm doing it again!");
});
server.listen(8887);