0.0.2 • Published 3 years ago

json22-express v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

JSON22-Express

Expressjs middleware providing support to JSON22 data format in your applications.

Features

  • Ready to use Express middleware
  • Parse JSON22 body content
  • Parse JSON body content
  • Support for deflate, gzip, br content encodings
  • Define Request.json22 method to send JSON22 encoded data
  • Zero-dependency npm-package
  • Both CJS/ESM modules support

Installation

npm install json22-express

In your application initialization file add as a middleware

import express from 'express'; 
import { json22express } from 'json22-express'

const app = express();
app.use(json22express());

For old-fashioned applications

const express = require('express'); 
const { json22express } = require('json22-express'); 

const app = express();
app.use(json22express());

Configuration

JSON22-Express middleware support set of configuration options.

Pass options

import express from 'express'; 
import { json22express } from 'json22-express'

const app = express();
app.use(json22express({
    overrideResponseJsonMethod: true,
    maxContentLength: 1024 * 1024, // 1 meg
}));

Options

OptionTypeDefaultDescription
handleJsonbooleanfalseParse JSON content as well as JSON22
maxContentLengthnumberno limitPrevent from parsing of too large payloads
keepRawAsstringdo not keep raw bodyDefine Request field name to save payload Buffer to
overrideResponseJsonMethodbooleanfalseOverride response json method as well
json22ParseOptionsJson22ParseOptionsemptyOptions to be passed to JSON22.parse() method
json22StringifyOptionsJson22StringifyOptionsemptyOptions to be passed to JSON22.stringify() method

Usage

import express from 'express'; 
import { json22express } from 'json22-express'

const app = express();

app.use(json22express({
    overrideResponseJsonMethod: true,
    maxContentLength: 1024 * 1024, // 1 meg
}));

app.get('/date', (req, res, next) => {
    // Use .json22() method from Response object
    res.status(200).json22({ date: new Date() });
    next();
});

app.get('/json', (req, res, next) => {
    // in case overrideResponseJsonMethod is set to true you may use 
    // .json() method to send JSON22 as well
    res.status(200).json({ date: new Date() });
    next();
});

app.get('/deprecated', (req, res, next) => {
    // WARNING: don't do this. It is deprecated method interface and in case
    // you set overrideResponseJsonMethod to true this method will throw an exception
    res.json(200, { date: new Date() });
    next();
});