Build a REST API Skeleton with Node.js

Tram Ho

Node.js is gaining popularity with microservice or REST Api applications because of their extremely fast performance and asynchronousness. Especially very compact and simultaneously handle many requests with super fast response time.

But regardless of the language we need to build a sturdy skeleton that can be followed to easily maintain and develop it. So today we share and build a skeleton for ourselves together.

Construct

Just like other REST API languages ​​we can also build skeletons in the form of MVC pattern. But in this article we only build to the API that only needs to use the controller and models combined with the middleware to handle requests and responses. You can refer to below:

The directory structure includes:

  • common : functions, contants common to the whole project.
  • controllers : navigational actions that process requests and responses.
  • database : contains config files for mongosee, mysql connection, etc …
  • middleware : contains rules to handle requests and responses.
  • models : contains models and data processing functions.
  • routes : contains functions that lack navigation with routes.

Init skeleton

Before building a REST API server with Express , we need to install some basic libraries as follows:

Since the database we will be using is mongo, we will need to use mongoose to access the database and will need env-cmd to load the .evn file (environment variables).

Database

mongoose.js contains mongodb connection configuration information.

Models

use.model.js contains Schema initialization information along with functions. It is completely possible to separate the schema into a separate directory for the code to be clearer and clearer.

Middleware

auth.middleware.js for security we need to verify the request again before allowing authorization to continue interacting by checking the token with the Authorization header.

Routes

index.js will play a role in loading the entire route configuration

auth.route.js will set up handler methods and direct them to controller – action. We will split into two types of routes, public and private, by setting the middleware for them.

Controllers

Here, we need to create functions corresponding to the actions as declared at auth.route.js . Actions play a role in navigating and processing the returned data before the response comes back.

auth.controller.js

Create server run app

Create app.js with the following content:

Since we need to process the data in json format, we will need a json parser with bodyParser . Then load the entire route with require('./routes/index');

Before testing, we need to reconfigure package.json so that the application can load environment variables with:

nodemon will be used as a tool to automatically reload the app after the successful save.

Next, we try to run the skeleton test. http: // localhost: 3000 /

Signup API

Login API

Conclude

Thus, we were able to create a simple skeleton with full of basic features such as login, signup, logout. In addition we can add ourselves a full skeleton with many other cool features. Hope the article will be useful to you guys in this language Newbie. All contributions will help the author improve in the next articles.

Share the news now

Source : Viblo