Validating and logging a NodeJS web API with Express-Validator and Bunyan.
This project was created to demonstrate two important processes in a NodeJS web API: validating parameters and logging. The source code presented is this repository is a modified version of this CRUD example, so be sure that MongoDB service is running in your machine.
For .Net developers, this tool will seem very similar to FluentValidation. Inside the '/validators' folder, the "UserValidator.js" file is responsible for validating the user's methods like POST and PUT.
The "/controllers/user.js" file's methods contain their respective validation array as the second parameter.
The "/validators/BaseValidator.js" file contains the "validate" function responsible for validating the parameters and returning an array of errors.
Inside the "server.js" file, Bunyan works as a middleware by intercepting requests and responses.
The "logger.js" file is responsible for writing the data into a log file named as the current date. If the file's size is greater than 3MB, it will be renamed containing the current time and another file will be created.
Each data is inserted as a Json Object inside the log file.
- Install packages:
npm install
- Run API:
npm run dev
- Acess URL:
http://localhost:3000