From ee48824cb8e46bf74ac004722ea013fb4398c6f9 Mon Sep 17 00:00:00 2001 From: Turgay Akbas Date: Tue, 2 Jul 2024 14:12:50 +0200 Subject: [PATCH] Add new endpoint for adding two numbers --- backend/src/server.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/backend/src/server.ts b/backend/src/server.ts index d21ef09..dcdab10 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -3,6 +3,9 @@ import express from 'express'; const app = express(); const PORT = process.env.PORT || 3000; +// Middleware to parse incoming request bodies +app.use(express.json()); + // Sample array of YouTube video URLs for demonstration const videoUrls = [ 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', @@ -19,6 +22,19 @@ app.get('/videos', (req, res) => { res.status(200).json(videoUrls); }); +// New POST endpoint to add two numbers +app.post('/addNumbers', (req, res) => { + const { num1, num2 } = req.body; + // Validate that both inputs are numbers + if (typeof num1 !== 'number' || typeof num2 !== 'number') { + return res.status(400).json({ error: 'Both inputs must be numbers' }); + } + // Calculate the sum + const sum = num1 + num2; + // Return the sum in a JSON response + res.json({ sum }); +}); + app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });