You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Utilizar nombres en lugar de verbos en las rutas de los puntos finales
constexpress=require('express');constbodyParser=require('body-parser');constapp=express();app.use(bodyParser.json());app.get('/articles',(req,res)=>{constarticles=[];res.json(articles);});app.post('/articles',(req,res)=>{res.json(req.body);});app.put('/articles/:id',(req,res)=>{const{ id }=req.params;res.json(req.body);});app.delete('/articles/:id',(req,res)=>{const{ id }=req.params;res.json({deleted: id});});app.listen(3000,()=>console.log('server started'));
Utilizar la anidación lógica en los puntos finales
Permitir el filtrado, la clasificación y la paginación
constexpress=require('express');constbodyParser=require('body-parser');constapp=express();// employees data in a databaseconstemployees=[{firstName: 'Jane',lastName: 'Smith',age: 20},//...{firstName: 'John',lastName: 'Smith',age: 30},{firstName: 'Mary',lastName: 'Green',age: 50},]app.use(bodyParser.json());app.get('/employees',(req,res)=>{const{ firstName, lastName, age }=req.query;letresults=[...employees];if(firstName){results=results.filter(r=>r.firstName===firstName);}if(lastName){results=results.filter(r=>r.lastName===lastName);}if(age){results=results.filter(r=>+r.age===+age);}res.json(results);});app.listen(3000,()=>console.log('server started'));