Skip to content

Validating and logging a NodeJS web API with Express-Validator and Bunyan.

Notifications You must be signed in to change notification settings

lucianopereira86/NodeJS-ExpressValidator-Bunyan

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

titulo

NodeJS-ExpressValidator-Bunyan

Validating and logging a NodeJS web API with Express-Validator and Bunyan.

Technologies:

Objectives

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.

Express-Validator

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.

code01

The "/controllers/user.js" file's methods contain their respective validation array as the second parameter.

code02

The "/validators/BaseValidator.js" file contains the "validate" function responsible for validating the parameters and returning an array of errors.

code03

Bunyan

Inside the "server.js" file, Bunyan works as a middleware by intercepting requests and responses.

code04

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.

code05

Each data is inserted as a Json Object inside the log file.

code06

To run this project

  • Install packages: npm install
  • Run API: npm run dev
  • Acess URL: http://localhost:3000

swagger

About

Validating and logging a NodeJS web API with Express-Validator and Bunyan.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages