Serving Routes
How to serve different routes
Use the app
object to define routes for different URLs. Routes are defined using HTTP methods (such as GET
, POST
, PUT
, DELETE
, etc.) and specify a callback function that gets executed when a request matches the specified URL and method.
const express = require('express');const app = express();const port = 3000;
// get routeapp.get('/', (req, res) => { res.send('Hello from GET route!');});
// post routeapp.post('/add', (req, res) => { res.send('Hello from POST route!');});
// PUT route - updationapp.put('/put/:id', (req, res) => { res.send('Hello from PUT route!');});
//DELETE routeapp.delete('/delete/:id', (req, res) => { res.send('Hello from DELETE route!');});
app.listen(port, () => { console.log(`Server is running on http://localhost:${port}`);});