Javascript Serer
// app.js
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware to parse incoming JSON payloads
app.use(express.json());
// HTTP GET route for the root path
app.get('/', (req, res) => {
res.send('Hello World!');
});
// HTTP GET route with a URL parameter
app.get('/api/users/:id', (req, res) => {
const userId = req.params.id;
res.json({ message: `Fetching data for user ${userId}`, status: 'success' });
});
// HTTP POST route to receive data
app.post('/api/data', (req, res) => {
const receivedData = req.body;
res.status(201).json({
message: 'Data successfully received',
data: receivedData
});
});
// Start the server and listen on the specified port
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});