This project is a simple Express application that allows you to manage user data by adding and retrieving users stored in a text file without interface.
- Initialize the Node.js project and install dependencies (
express
,fs
). - Configure the server with Express.
- POST
/users
: Add users by providing their name and surnames in JSON format. Validate inputs and store them in a text file. - GET
/users
: Retrieve users from the file, with optional filters for name and surnames. Return all users if no filters are provided.
- Implement clear error messages for input validation and file handling issues.
- Consider using file handling techniques like streams for better performance with large data files.
-
Clone the repository:
git clone https://github.com/crimanlor/Api-Rest-Express.git
-
Navigate to the project directory:
cd your-repo
-
Install dependencies:
npm install
-
Start the server:
npm start
- Open your browser and go to
http://localhost:3000
. - Use a tool like Postman or ThunderClient to make requests to the POST and GET endpoints.
- Add new users using the POST
/users
endpoint and view all stored users using the GET/users
endpoint.
express
: To create the server and handle routes.morgan
: To log requests.fs
: To handle file operations (reading and writing user data).path
: To handle the absolute and relative routes.
You can use the following code snippets in Postman or another HTTP client:
Add a new user:
POST /users
Content-Type: application/json
{
"name": "Lorena",
"surnames": "Criado Gonzalez"
}
Get all users:
GET /users
Get user filtered by name:
GET /users?name=Lorena
Get user filtered by surnames:
GET /users?surnames=Criado
Get users filtered by both name and surnames
GET /users?name=Lorena&surnames=Criado