An opinionated express middleware just extend res.json.
npm install express-jsonx
const express = require('express')
const jsonx = require('express-jsonx')
const app = express()
app.use(jsonx())
app.get('/', function (req, res) {
res.jsonx(1, 'Hi~')
})
res.jsonx(1)
<=>res.json({code: 0, data: 1})
res.jsonx({demo: true})
<=>res.json({code: 0, data: {demo: true}})
res.jsonx(1, 'text')
<=>res.json({code: 1, data: 'text'})
res.jsonx(-1, '', 'fail')
<=>res.json({code: -1, data: '', msg: 'fail'})
app.use(jsonx(options))
name | type | default | note |
---|---|---|---|
codeKey | string | code |
key of code |
dataKey | string | data |
key of data |
msgKey | string | msg |
key of message |
defaultCode | number | 0 |
default code |
MIT(./LICENSE)