codex-lv4-may-2025

Level Navigation: 1 2 3 (4ℹ️) (5ℹ️) 6 7 8 9 10 11 12 13 14⚡ 15⚡ (16ℹ️) 17 18 19 20 21 22 23 24 25 26⚡ 27⚡ 28⚡ 29 30 31 32 33 34 35 36 37 38 39⚡ 40⚡

Level 3: Create Your First Express Server

  1. Create your project structure:
    • Create a src directory
    • Create src/index.js as your main server file

    Show Me: starter Express server code

    // src/index.js
    import express from 'express';
       
    const app = express();
    const port = 3000;
       
    app.get('/', (req, res) => {
      res.send('Hello World!');
    });
       
    app.listen(port, () => {
      console.log(`Server listening on port ${port}`);
    });
    

    See: Express Hello World Example for a minimal Express app example.

Now you’re ready to start building your Express server!

Try It!

Test your server setup:

  1. Start the development server:
    npm run dev
    
    • The server should start and display “Server listening on port 3000”
    • Open http://localhost:3000 in your browser
    • You should see “Hello World!” displayed
    • Try making a change to src/index.js (like changing the message) and save—nodemon will automatically restart the server!
  2. Stop the server (press Ctrl+C in your terminal)

  3. Start the production server:
    npm start
    
    • The server starts the same way, but without auto-restart
    • Make a change to your code and save—notice the server doesn’t restart automatically
    • Stop the server again (Ctrl+C)

💡 Tip: Use npm run dev during development for the auto-restart feature, and npm start when you want to test the production behavior.