const express = require('express');
const bodyParser = require('body-parser'); // Se utiliza para analizar los datos del cuerpo de la solicitud en formatos como JSONconst app = express();
const port = 3500;
app.use(bodyParser.json());
// Base de datos simuladalet peliculas = [
{ id: 1, titulo: 'Taxi Driver', director: 'Martin Scorsese' },
{ id: 2, titulo: 'El crepúsculo de los dioses', director: 'Billy Wilder' },
{ id: 3, titulo: 'Pobres criaturas ', director: 'Emma Stone' }
];
// Obtener todas las películas
app.get('/v1/peliculas', (req, res) => {
res.json(peliculas);
});
// Obtener una película por su ID
app.get('/v1/peliculas/:id', (req, res) => {
const id = parseInt(req.params.id);
const pelicula = peliculas.find(pelicula => pelicula.id === id);
if (pelicula) {
res.json(pelicula);
} else {
res.status(404).send('Pelicula no Encontrada');
}
});
// Agregar una nueva película
app.post('/v1/peliculas', (req, res) => {
const { titulo, director } = req.body;
const id = peliculas.length + 1;
const newpelicula = { id, titulo, director };
peliculas.push(newpelicula);
res.status(201).json(newpelicula);
});
// Actualizar una película existente
app.put('/v1/peliculas/:id', (req, res) => {
const id = parseInt(req.params.id);
const { titulo, director } = req.body;
const index = peliculas.findIndex(pelicula => pelicula.id === id);
if (index !== -1) {
peliculas[index] = { id, titulo, director };
res.json(peliculas[index]);
} else {
res.status(404).send('Pelicula no Encontrada');
}
});
// Eliminar una película
app.delete('/v1/peliculas/:id', (req, res) => {
const id = parseInt(req.params.id);
const index = peliculas.findIndex(pelicula => pelicula.id === id);
if (index !== -1) {
peliculas.splice(index, 1);
res.send('pelicula eliminado exitosamente');
} else {
res.status(404).send('Pelicula no Encontrada');
}
});
app.listen(port, () => {
console.log(`Server en el puerto ${port}`);
});
{
"info": {
"_postman_id": "1c2180a6-9501-41d1-a4dc-e05bc928f62d",
"name": "ExamplePlatziCurso",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "33152367"
},
"item": [
{
"name": "GetAllPeliculas",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:3500/v1/peliculas",
"protocol": "http",
"host": [
"localhost"
],
"port": "3500",
"path": [
"v1",
"peliculas"
]
}
},
"response": []
},
{
"name": "GetOnePelicula",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:3500/v1/peliculas/1",
"protocol": "http",
"host": [
"localhost"
],
"port": "3500",
"path": [
"v1",
"peliculas",
"1"
]
}
},
"response": []
},
{
"name": "PostPelicula",
"request": {
"method": "POST",
"header": [
{
"key": "Accept",
"value": "application/json",
"type": "text"
},
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"titulo\": \"Titulo Otra Pelicula\", \r\n \"director\": \"Otro Nombre\" \r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:3500/v1/peliculas",
"protocol": "http",
"host": [
"localhost"
],
"port": "3500",
"path": [
"v1",
"peliculas"
]
}
},
"response": []
},
{
"name": "PutPelicula",
"request": {
"method": "PUT",
"header": [
{
"key": "Accept",
"value": "application/json",
"type": "text"
},
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"titulo\": \"Titulo Otra Pelicula\", \r\n \"director\": \"Cambie de Nombre\" \r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:3500/v1/peliculas/4",
"protocol": "http",
"host": [
"localhost"
],
"port": "3500",
"path": [
"v1",
"peliculas",
"4"
]
}
},
"response": []
},
{
"name": "DeletePelicula",
"request": {
"method": "DELETE",
"header": [],
"url": {
"raw": "http://localhost:3500/v1/peliculas/4",
"protocol": "http",
"host": [
"localhost"
],
"port": "3500",
"path": [
"v1",
"peliculas",
"4"
]
}
},
"response": []
}
]
}
Con todo esto ya podras hacer pruebas desde Postman