Student Activity

Student Activity (30-min)

Activity: Building and Testing a Basic HTTP Server

Step-by-Step Procedure:

  1. Step 1: Set Up a Basic HTTP Server:

    • Create a file named basicServer.js and write the following code:

      const http = require('http');
      
      const server = http.createServer((req, res) => {
          res.writeHead(200, { 'Content-Type': 'application/json' });
          res.end(JSON.stringify({ message: 'Hello, World!' }));
      });
      
      server.listen(3000, () => {
          console.log('Server running at http://localhost:3000');
      });
  2. Step 2: Run the Server:

    • Open a terminal, navigate to the file location, and run:

      // bash
      node basicServer.js
  3. Step 3: Test the Server with Postman:

    • Open Postman.

    • Send a GET request to http://localhost:3000.

    • Inspect the response body (JSON format) and headers.

  4. Step 4: Test with a Browser:

    • Open a browser and visit http://localhost:3000.

    • Observe the output as JSON in the browser window.

Last updated