request-to-json
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

request-to-json Build Status

Returns a JSON representation of request (supports koa/express requests props)

Installation

npm i --save request-to-json

Usage

Supports both ESM and CommonJS

// esm
import reqToJSON from 'request-to-json'
// commonjs
const reqToJSON = require('request-to-json')

HTTP server request to JSON

import http from 'http'
import reqToJSON from 'request-to-json'
 
http.createServer(function (req) {
  reqToJSON(req)
  /*
  {
    "aborted": false,
    "complete": true,
    "headers": Object {
      "connection": "close",
      "host": "localhost:3033",
    },
    "httpVersion": "1.1",
    "method": "GET",
    "trailers": Object {},
    "url": "/",
  }
  */
})

Express request to JSON

import express from 'express'
import reqToJSON from 'request-to-json'
 
const app = express()
app.get('user/:id', function (req) {
  reqToJSON(req)
  /*
  {
    "aborted": false,
    "baseUrl": "",
    "complete": true,
    "fresh": false,
    "headers": Object {
      "connection": "close",
      "host": "localhost:3033",
    },
    "host": "localhost",
    "hostname": "localhost",
    "httpVersion": "1.1",
    "ips": Array [],
    "method": "GET",
    "originalUrl": "/",
    "params": Object {},
    "path": "/",
    "protocol": "http",
    "query": Object {},
    "secure": false,
    "stale": true,
    "subdomains": Array [],
    "trailers": Object {},
    "url": "/",
    "xhr": false,
  }
  */
})

Koa request to JSON

import koa from 'koa'
import reqToJSON from 'request-to-json'
 
const app = new Koa()
app.use(async function (ctx, next) {
  reqToJSON(ctx)
  /*
  {
    "body": "hello world",
    "cookies": Object {
      "secure": false,
    },
    "fresh": false,
    "headers": Object {
      "connection": "close",
      "host": "localhost:3033",
      "x-custom": "custom",
    },
    "host": "localhost:3033",
    "hostname": "localhost",
    "href": "http://localhost:3033/",
    "ip": "",
    "ips": Array [],
    "method": "GET",
    "origin": "http://localhost:3033",
    "originalUrl": "/",
    "path": "/",
    "protocol": "http",
    "query": Object {},
    "secure": false,
    "stale": true,
    "subdomains": Array [],
    "url": "/",
  }
  */
  reqToJSON(ctx.request)
  /*
  {
    "fresh": false,
    "headers": Object {
      "connection": "close",
      "host": "localhost:3033",
      "x-custom": "custom",
    },
    "host": "localhost:3033",
    "hostname": "localhost",
    "href": "http://localhost:3033/",
    "ip": "",
    "ips": Array [],
    "method": "GET",
    "origin": "http://localhost:3033",
    "originalUrl": "/",
    "path": "/",
    "protocol": "http",
    "query": Object {},
    "secure": false,
    "stale": true,
    "subdomains": Array [],
    "url": "/",
  }
  */
})

License

MIT

Package Sidebar

Install

npm i request-to-json

Weekly Downloads

2

Version

2.0.2

License

MIT

Unpacked Size

47 kB

Total Files

18

Last publish

Collaborators

  • tjmehta