Routemaster

Setup a Basic Express Server with Routing

  1. Install Express: Make sure you have Node.js installed. Then create a new directory for your project and install Express:
mkdir my-routemaster cd my-routemaster npm init -y npm install express
  1. Create the Server: Create a file named server.js:
const express = require('express'); const app = express(); const PORT = 3000; // Define routes app.get('/', (req, res) => { res.send('Welcome to the Routemaster!'); }); app.get('/about', (req, res) => { res.send('This is the about page.'); }); app.get('/contact', (req, res) => { res.send('This is the contact page.'); }); // Start the server app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
  1. Run the Server: Start your server by running:
node server.js
  1. Access the Routes: Open your browser and go to:
    • http://localhost:3000/ for the home page
    • http://localhost:3000/about for the about page
    • http://localhost:3000/contact for the contact page

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *