Student Activity

Student Activity (30-min)

Activity: Building a Basic Express Application

Step-by-Step Procedure:

  1. Step 1: Initialize an Express Project:

    • Create a new folder for the project:

      bash코드 복사mkdir express-app
      cd express-app
      npm init -y
      npm install express
  2. Step 2: Set Up the Application:

    • Create a file app.js and add the following:

      javascript코드 복사const express = require('express');
      const app = express();
      
      // Middleware to log requests
      app.use((req, res, next) => {
          console.log(`${req.method} ${req.url}`);
          next();
      });
      
      // Define routes
      app.get('/', (req, res) => res.send('Welcome to the Home Page'));
      app.get('/about', (req, res) => res.send('About Us'));
      app.post('/data', (req, res) => res.send('Data received'));
      
      // Start the server
      const PORT = 3000;
      app.listen(PORT, () => {
          console.log(`Server running on http://localhost:${PORT}`);
      });
  3. Step 3: Test the Application:

    • Start the server:

      bash코드 복사node app.js
    • Use Postman or a browser to test:

      • GET http://localhost:3000/

      • GET http://localhost:3000/about

      • POST http://localhost:3000/data


Last updated