This blog is simple tutorial to create a simple api which can also be used as a microservice or individually for authenticating a user in any application. This api tutorial also covers authentication using JWT and user verification using an alphanumeric OTP.
The above structure follows a Model View Controller(MVC) pattern.
The /bin folder contains the the code to run the http server in order to execute the api.
The /common folder contains all the shared code and resources in the api i.e constants, status, error codes and messages, middleware like loggers, interceptors, authentication for other services, plugins schema attribute injections i.e mongoose plugins, and utility scripts and other small parsing functions.
The /config folder contains all the configuration variables used across the project.
The /connections folder contains the connections to the database.
The /controllers folder contains all the controllers i.e in this case the userControllers which controls the request and the response exchange interface.
The /models folder contains the core schema of the user document which will be saved in the database with all the respective types and properties.
The /public folder contains a basic stylesheet for the .pug files in /views and the /views contains a basic .pug template which can be used to check if the API is up and running.
The /routes folder contains all the routing for the requests using express to the right validators and controllers.
The /services part contains all the services used in the project i.e mongo, mailer and a custom query builder. I have written a custom query builder to ensure better reuse of code for large projects. For small projects, the contents of the query builder can be a part of the controller.
The /validators folder contains all the request validators which validate every field of the request using express-joi-validation and joi.
The /test folder which contains all the test cases for integration and unit tests with mock data. The folder contains only a sample test case for the register route.
The app.js file contains all the entry point config regarding, middlewares, configs, cors, custom headers etc.
Note: This api uses an automated mailer api. You can find the mailer api git repository here and also can find the blog here.
If you would like to access the github repository, you can find it here.
Written by : Suprith Reddy
I am a serial tech entrepreneur. I believe entrepreneurship is my purpose and technology is my passion. I am naturally business savvy and my expertise is building digital products from scratch and scaling them to create impact. I give talks about technology, business and entrepreneurship.
Subscribe To My Newsletter
BE NOTIFIED ABOUT NEW ARTICLES OR EVENTS OR SESSIONS
You can get access to content with other group of entrepreneurs and startup folks.