Introducci贸n a Node.js y Express
Node.js y Express
Entorno de trabajo
Variables de entorno
Rutas din谩micas
Body parser
Postman
CRUD
驴Que es una API Restful?
Solicitudes GET
Solicitudes POST
CRUD
Soluci贸n del reto de validaci贸n
Solicitudes DELETE
MIddlewares
Middlewares - logger
Middlewares - ErrorHandler
DB
Instalar Postgresql
Instalar Prisma y PostgreSQL Client
Prisma Models
JWT
Autenticaci贸n utilizando JSON Web Tokens (JWT)
Auth - Register
Auth - Login
Expres.js
Arquitectura
Arquitectura parte 2
Creaci贸n y Migraci贸n de Modelos con Prisma para Citas M茅dicas
Admin controllers / services
Admin controllers / services parte 2
Reservations
Reservations parte 2
Appointments
Deploy
PostgreSQL
Deploy
You don't have access to this class
Keep learning! Join and start boosting your career
Data deletion is a fundamental operation in any application that handles information. In the context of web development, correctly implementing the delete functionality is essential to maintain data integrity and provide a complete user experience. This time, we will explore how to implement the DELETE operation in our CRUD (Create, Read, Update, Delete) using Node.js and file handling for local storage.
To complete our CRUD, we need to implement the user delete functionality. This operation will allow us to delete specific records from our local file-based database. Let's create a new entry point in our application that will handle DELETE requests.
First, we create our file to handle the deletion:
// app.delete.jsapp.delete('/users/:id', (req, res) => { const userId = parseInt(req.params.id, 10);
fs.readFile(usersFilePath, 'utf8', (err, data) => { if (err) { return res.status(500).json({ error: "Error with data connection" }); }
let users = JSON.parse(data); users = users.filter(user => user.id !== userId);
fs.writeFile(usersFilePath, JSON.stringify(users, null, 2), (err) => { if (err) { return res.status(500).json({ error: "Error deleting user" }); }
res.status(204).send(); }); }); });});
In this code:
/users/:id
that accepts the ID of the user to delete.The key part of our implementation is the filter()
method that we use to remove the specific user:
users = users.filter(user => user.id !== userId);
This method creates a new array that includes all the elements that pass the specified condition. In our case, we are keeping all the users whose ID does not match the ID we want to remove. In this way, we effectively remove the desired user from the array.
It is important to understand that this approach does not modify the original array, but creates a new one without the element we want to remove. This follows the principle of immutability which is a good programming practice.
To test our implementation, we use Postman:
/users/2
)If everything works correctly, the user with the specified ID will be removed from our users file.
With the implementation of DELETE, we now have a complete CRUD for our application:
Each of these operations follows a similar pattern:
As mentioned in the class, there is one remaining challenge related to the update (PUT) functionality. The problem is related to the handling of duplicate IDs. The solution involves modifying the update logic to ensure that:
This is an excellent exercise to practice data manipulation and programming logic.
Once we have our basic CRUD up and running, we can move on to more advanced concepts such as:
These topics will allow us to build more robust, secure and maintainable applications.
Implementing a complete CRUD is an important milestone in web application development. With these basic operations, we can build complex systems that allow users to interact with data effectively. Have you implemented any interesting solutions to the upgrade challenge? Share your ideas and solutions in the comments.
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?