Setup a Basic Express Server with Routing
- 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
- 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}`); });
- Run the Server: Start your server by running:
node server.js
- 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
Leave a Reply