Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

express-json-success

1.2.2 • Public • Published

express-json-success

JSON API response format for Express

This module creates a JSON response such as the following.

{
  "success": true,
  "message": "all done",
  "result": "anythings you want",
  "time": "2017-08-30T09:59:00.846Z",
  "code": 200
}

More info on JSON Success Response

Installation

npm install express-json-success --save

Example

Basic usage

const success = require('express-json-success');
const express = require('express');
const app = new express();
 
success(app);
 
app.use((req, res)=>{
    res.success(true);
    /* Response output example 
    {
      "success": true,
      "code": 200,
      "message": "ok",
      "result": null,
      "time": "2017-08-30T09:59:00.846Z"
    }
    */
    
    res.success(
        true,
        'message string', // default is "ok" or "failed"
        'a result that can be anythings', // default is null
        200 //status code, default 200
    );
});
 
app.listen(3000);

API

  • success(success, [message], [result], [code])
  • successTrue([message], [result], [code])
  • successFalse([message], [result], [code])
  • successIf(result, [opts])
  • successIfNotEmpty(result, [opts])
  • success400([message], [result])
  • success401([message], [result])
  • success402([message], [result])
  • success403([message], [result])
  • success404([message], [result])
  • success405([message], [result])
  • success406([message], [result])
  • success407([message], [result])
  • success408([message], [result])
  • success409([message], [result])
  • success410([message], [result])
  • success411([message], [result])
  • success412([message], [result])
  • success413([message], [result])
  • success414([message], [result])
  • success415([message], [result])
  • success416([message], [result])
  • success417([message], [result])
  • success418([message], [result])
  • success417([message], [result])
  • success421([message], [result])
  • success422([message], [result])
  • success423([message], [result])
  • success424([message], [result])
  • success425([message], [result])
  • success426([message], [result])
  • success428([message], [result])
  • success429([message], [result])
  • success431([message], [result])
  • success500([message], [result])
  • success501([message], [result])
  • success502([message], [result])
  • success503([message], [result])
  • success504([message], [result])
  • success505([message], [result])
  • success506([message], [result])
  • success507([message], [result])
  • success508([message], [result])
  • success509([message], [result])
  • success510([message], [result])
  • success511([message], [result])

Please see full documentation

Changelog

You can view the changelog here

License

express-json-success is open-sourced software licensed under the MIT license

Authors

Fabio Ricali

Install

npm i express-json-success

DownloadsWeekly Downloads

0

Version

1.2.2

License

MIT

Last publish

Collaborators

  • avatar