DEV Community

dhamolahedonist
dhamolahedonist

Posted on

Design a blog API with Express, MongoDb and Heroku

Several articles have been written about using NodeJs, Heroku, and the Express framework to build straightforward blogs. Here is yet another viewpoint and way of thinking that goes in the same direction.

The most widely used NodeJs framework, Express, offers a number of abstracted functionalities for creating straightforward yet effective APIs.

The blog initially uses APIs to handle articles for certain authors. Articles can be managed by authors as private or public.

Prerequisites
You will be required to have Node JS and npm before starting. Make sure you already have them installed. If not you can find them here: https://nodejs.org. Additional tools like Mongodb and Heroku were utilized. If you'd want more specific instructions on how to use these tools, please refer to their documentation.

Packages and Tools

A number of packages, including Joi, bcryptjs, Mongoose, Passport, and jwt, are used by the application. For detailed usage information, please consult the documentation for these packages.

In order to ensure that the payload is legitimate before being saved in the database, Joi is utilized in Post endpoints for payload validation. Bcryptjs was used to encrypt the data, including passwords, before it was put in the database.

Mongoose is the preferred ORM to communicate with the database because we are using MongoDB. For authentication and authorization, the program additionally uses the passport and jwt packages.

Folder Structure
Controllers
The controllers, which houses the business logic, is included in this.

  1. The authenticationController.js file contains the middleware for "signup" and "login." The "singup" middleware transmits the user's information to the succeeding middleware after saving the user's information to the database. If not, an error is reported.

The "login" middleware verifies the user's identity using the specified email and password.
If the user is located, the following middleware receives the user's information.
If not, it reports an error.

The following actions can be performed by the blogController.js: create a blog, delete a blog by an id produced by a certain user, alter blog properties by an id by the person who created the blog, get a blog by an id, increase the read count of the blog when viewed, etc.
Additionally, it determines how long it takes to read a blog and retrieves every blog in the database.

The userController.js is capable of carrying out these operations: retrieve a user by an id, change user properties by the user's id.

Middleware
The validation is housed in the middleware.
Before signing up, the user must supply all required information, while the validateUser.js script checks to see if a token is included in the authorization headers before creating a blog. meaning that before publishing a blog post, the user must have registered and logged in.

Models
All of the input needed to create a blog post is specified in the BlogModel.js file.
The userModel.js file outlines all the information needed to create a user.

Routes
Before adding the user to the database, authRoute.js verifies that the user has provided the necessary information. It also verifies that the user's information matches that in the database when the user logs in before issuing a token to the user.

When a specific endpoint defined in the route is being called, the blogRoute.js merely applies the business logic defined in the blogContoller.js,
When a certain endpoint specified in the route is being called, the userRoute.js merely implements the business logic provided in the userContoller.js.

db.js
All user and blog data is kept in the mongdb database, which is connected to using db.js.

.env.example
environment variable offers all the configuration-related data needed.

app.js
All of the application's files are combined into one file called app.js.

The ReadMe file contains instructions on how to install the program and describes its features.

Deployment on heroku

  • Install the heroku CLI so that it is accessible in the terminal. Log in to the app that will be deployed using the heroku CLI, for example, cd blog-api. Type "heroku login" and then "heroku create" in the cmd window. -Next, depending on the branch, type "git push heroku main" or "git push heroku master". Type "git branch" to find out the branch you are now editing.

Conclusion
Altschool Africa assigned this application as part of their grade. Please visit https://github.com/dhamolahedonist/BloggingApi if you think the software needs further features.

Top comments (0)