Giter Site home page Giter Site logo

http-z's Introduction

http-z

Build Status Code Coverage npm version

Features

HTTP message (request/response) parser/builder according to the rules defined in RFC 7230. Works in Node.js and in the browser.

Install

$ pnpm i http-z

Usage

const httpZ = require('http-z')

let rawMessage = [
  'GET /features?p1=v1 HTTP/1.1',
  'Host: example.com',
  'Accept: *',
  'Accept-Encoding: gzip,deflate',
  'Accept-Language: en-US;q=0.6, en;q=0.4',
  '',
  ''
].join('\r\n')

let messageModel = httpZ.parse(rawMessage)
console.log(JSON.stringify(messageModel, null, 2))

/* output:
{
  "protocolVersion": "HTTP/1.1",
  "method": "GET",
  "target": "/features?p1=v1",
  "host": "example.com",
  "path": "/features",
  "headersSize": 135,
  "bodySize": 0,
  "queryParams": [
    { "name": "p1", "value": "v1" }
  ],
  "headers": [
    { "name": "Host", "value": "example.com" },
    { "name": "Accept", value": "*" },
    { "name": "Accept-Encoding", "value": "gzip,deflate" },
    { "name": "Accept-Language", "value": "en-US;q=0.6, en;q=0.4" }
  ]
}
*/

rawMessage = httpZ.build(messageModel)
console.log(rawMessage)

/* output:
GET /features?p1=v1 HTTP/1.1
Host: example.com
Accept: *
Accept-Encoding: gzip,deflate
Accept-Language: en-US;q=0.6, en;q=0.4


*/

API

parse(rawMessage, opts)

Parses HTTP request/response raw message and returns a model.

  • rawMessage is HTTP raw message.
  • opts - options object, can be skipped.
build(messageModel, opts)

Builds HTTP request/response raw message from the model.

  • messageModel is HTTP message model.
  • opts - options object, can be skipped.

References

Licence

Licensed under the MIT license.

Author

Alexander Mac

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.