Skip to content
Snippets Groups Projects
Select Git revision
  • e4ff624cc146b413fe779b13d678c4df46c96c50
  • master default protected
2 results

server.js

Blame
  • server.js 731 B
    const express = require("express");
    const app = express();
    
    // Middleware to parse JSON data
    app.use(express.json());
    
    const PORT = 3000;
    
    // GET API - Returns path param & query param
    app.get("/user/:id", (req, res) => {
        const pathParam = req.params.id;
        const queryParam = req.query.name;
    
        res.json({
            message: "GET API Response",
            pathParam: pathParam,
            queryParam: queryParam,
        });
    });
    
    // Array to store POST data
    const dataStore = [];
    
    app.post("/data", (req, res) => {
        const receivedData = req.body;
        
        dataStore.push(receivedData);
    
        res.json(dataStore);
    });
    
    // Start the server
    app.listen(PORT, () => {
        console.log(`Server is running on http://localhost:${PORT}`);
    });