This article has been published by netease Cloud community authorized by the author Wang Zhenhua.

Welcome to visit netease Cloud Community to learn more about Netease’s technical product operation experience.


I’ve been developing and maintaining API systems, large and small, with Restify during my career, and I’d like to share some Tips that I think are good.

Take advantage of the Middleware mechanism

Function (req, res, next) {}, function (req, res, next) {} These handler functions are then stacked together to form a linear model to complete the life cycle of a request.

First, take a look at the core skeleton of a Resitfy Api application

let restify = require('restify')let app = restify.createServer()
app.use(restify.plugins.queryParser())
app.use(restify.plugins.bodyParser())
app.get('/api/users/list', listUsers)Copy the code

Using the Middleware mechanism to load two plug-ins, and then using your own listUsers function on your own route, it’s pretty straightforward.

Wait, listUser doesn’t have to be a handler function, it can actually be a handler chain(array of functions). Based on this simple idea, we can handle some common tasks in Web development more elegantly.

Parameter validation

Parameter verification is an essential step in API programming. Validation libraries are usually used to improve the daily experience, for example:

  let saveGiftRecord = [
    paramsValidator({
      roleid: Joi.number().integer().required(),
      friend_id: Joi.number().integer().required(),
      gift_type: Joi.number().integer().only(giftTypes).required()
    }),    function saveGiftRecord (req, res, next) {      //DO SOMETHING
    }
  ]Copy the code

We use Joi as the validation library, and use the paramsValidator as the helper function to generate a handler function to make everyday parameter validation more enjoyable. In addition, because parameter validation exists as a separate function, you gain additional gains that can be easily shared between different API endpoints.

Time consuming to track

Restify provides a set of apis for developing elapsed time statistics for handler functions using startHandlerTimer, endHandlerTimer, and when you pass it to a route in the form of a handlers chain array, it records the elapsed time of all handlers. It helps to analyze performance bottlenecks when API performance is poor

Open it using the built-in plug-in auditLogger

[2017-12-25T22:15:09.488+08:00] INFO: push-API /61713 on Zhenhua. local: (req_id= a29AD32e-7BF6-4131-8fbf-d630b4af5f34, latency=88) GET /users/followed/list? Roleid = 101150001 HTTP / 1.1 the req. Timers: {"parseCookies": 226,
      "parseQueryString": 1792,
      "readBody": 395,
      "parseBody": 130,
      "getAuthByType": 129,
      "authMiddleware": 4506,
      "validateParams": 381,
      "getFollowList": 62657,
      "getFanCountBatch": 3140,
      "filterSelfFollowIds": 2182,
      "filterSelfFanIds": 107,
      "getRoleInfos": 1736,
      "formatResult": 640,
      "sendResponse": 4718}Copy the code

As shown in the figure, the time spent by each handler can be seen in the auditLogger to facilitate locating bottlenecks.

These two apis use process.hrtime() for high time accuracy

Also of interest is the restify repository’s next library, Conductor, which makes it easy to compose handlers


Netease Cloud Free experience pavilion, 0 cost experience 20+ cloud products!

For more information about netease’s technology, products and operating experience, please click here.


Master a few skills in 3 minutes: making dynamic headlines