Created
September 5, 2026 08:57
-
-
Save thinkphp/b80a6f8327a329cbf51018416fb3a791 to your computer and use it in GitHub Desktop.
server express pentru Factorial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express') | |
| const path = require('path') | |
| const app = express() | |
| const PORT = 3000; | |
| //functie pentru a calcula factorialul | |
| function factorial(num) { | |
| if(num < 0) return "nu se poate calcula factorialul din numere negative" | |
| let result = 1; | |
| for(let i = 1; i <= num; i++) { | |
| result *= i; | |
| } | |
| return result; | |
| } | |
| //serveste pagina HTML | |
| app.get('/', (req, res) => { | |
| res.sendFile(path.join(__dirname, 'index.html')) | |
| }) | |
| app.get('/factorial', {req, res} => { | |
| const { number } = req.query; | |
| if(number && !isNaN(number)) { | |
| const num = parseInt( number ); | |
| const result = factorial( num ) | |
| res.json( {result} ) | |
| } else { | |
| res.status(400).json({error: 'Te rog sa furnizezi un numar valid prin parametrul "number"}') | |
| } | |
| }) | |
| app.listen(PORT, 'localhost', () => { | |
| console.log("Serverul ruleaza la http://localhost: ${PORT}") | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment