Build RESTful API
Web frameworks are widely used for providing interfaces to the client through Web services. Let's use an example CNode Club to show how to build RESTful API using Egg.
CNode currently use v1 interface is not fully consistent with the RESTful semantic. In the article, we will encapsulate a more RESTful semantic V2 API based on CNode V1 interface.
# Response Formatting
Designing a RESTful-style API, we will identify the status of response by the response status code, keeping the response body simply and only the interface data is returned.
A example of topics
is shown below:
# Get topics list
GET /api/v2/topics
- status code: 200
- response body:
[ |
# Retrieve one topic
GET /api/v2/topics/57ea257b3670ca3f44c5beb6
- status code: 200
- response body:
{ |
# Create topics
POST /api/v2/topics
- status code: 201
- response body:
{ |
# Update topics
PUT /api/v2/topics/57ea257b3670ca3f44c5beb6
- status code: 204
- response body: null
# Error handling
When an error is occurring, 4xx status code is returned if occurred by client-side request parameters and 5xx status code is returned if occurred by server-side logic processing. All error objects are used as the description for status exceptions.
For example, passing invalided parameters from the client may return a response with status code 422, the response body as shown below:
{ |
# Getting Started
After interface convention, we begin to create a RESTful API.
# Application initialization
Initializes the application using egg-init in the quickstart
$ egg-init cnode-api --type=simple |
# Enable validate plugin
egg-validate is used to present the validate plugin.
// config/plugin.js |
# Router registry
First of all, we follower previous design to register router. The framework provides a simply way to create a RESTful-style router and mapping the resources to the corresponding controllers.
// app/router.js |
Mapping the 'topics' resource's CRUD interfaces to the app/controller/topics.js
using app.resources
# Developing controller
In controller, we only need to implement the interface convention of app.resources
RESTful style URL definition. For example, creating a 'topics' interface:
// app/controller/topics.js |
As shown above, a Controller mainly implements the following logic:
- call the validate function to validate the request parameters
- create a topic by calling service encapsulates business logic using the validated parameters
- configure the status code and context according to the interface convention
# Developing service
We will more focus on writing effective business logic in service.
// app/service/topics.js |
After developing the Service of topic creation, an interface have been completed from top to bottom.
# Unified error handling
Normal business logic has been completed, but exceptions have not yet been processed. Controller and Service may throw an exception as the previous coding, so it is recommended that throwing an exception to interrupt if passing invalided parameters from the client or calling the back-end service with exception.
- use Controller
this.ctx.validate()
to validate the parameters, throw exception if it fails. - call Service
this.ctx.curl()
to access CNode API, may throw server exception due to network problems. - an exception also will be thrown after Service is getting the response of calling failure from CNode API.
Default error handling is provided but might be inconsistent as the interface convention previously. We need to implement a unified error-handling middleware to handle the errors.
Create a file error_handler.js
under app/middleware
directory to create a new middleware
// app/middleware/error_handler.js |
We can catch all exceptions and follow the expected format to encapsulate the response through the middleware. It can be loaded into application using configuration file (config/config.default.js
)
// config/config.default.js |
# Testing
Completing the coding just the first step, furthermore we need to add Unit Test to the code.
# Controller Testing
Let's start writing the unit test for the Controller. We can simulate the implementation of the Service layer in an appropriate way because the most important part is to test the logic as for Controller. And mocking up the Service layer according the convention of interface, so we can develop layered testing because the Service layer itself can also covered by Service unit test.
const { app, mock, assert } = require('egg-mock/bootstrap'); |
As the Controller testing above, we create an application using egg-mock and simulate the client to send request through SuperTest. In the testing, we also simulate the response from Service layer to test the processing logic of Controller layer
# Service Testing
Unit Test of Service layer may focus on the coding logic. egg-mock provides a quick method to test the Service by calling the test method in the Service, and SuperTest to simulate the client request is no longer needed.
const { app, mock, assert } = require('egg-mock/bootstrap'); |
In the testing of Service layer above, we create a Context object using the app.createContext()
which provided by egg-mock and call the Service method on Context object to test directly. It can use app.mockHttpclient()
to simulate the response of calling HTTP request, which allows us to focus on the logic testing of Service layer without the impact of environment.
See the full example at eggjs/examples/cnode-api.