2.0.8 • Published 6 years ago

@lbzg/http-help v2.0.8

Weekly downloads
1
License
ISC
Repository
bitbucket
Last release
6 years ago

Small nodejs http helper.

Installation & Usage

npm i @lbzg/http-help
const http = require('http')
const hh = require('@lbzg/http-help')
 
const port = 3337

function handler(req, res) {
  # assume GET http://this.server/home/sub?slider=27 came in
  
  console.log(hh.getPath(req)) # ['home', 'sub']
  console.log(hh.getPath(req, 0)) # 'home'
  console.log(hh.getQuery(req)) # {'slider': '27'}

  hh.setMime(res, 'text') # sets mime type 'text/plain'
  hh.setCors(res, 'http://foo.example')   # setCors(res, origin, methods, headers, ttl)
  hh.respond(req, res, 200, 'some data') # respond with code 200 and some data
}

http.createServer(handler).listen(port)

Methods

getPath(req, index = null)
getQuery(req, stringFormat = false)
getPostBody(req)
setCors(res, origin = null, methods = null, headers = null, ttl = null)
setMime(res, mime = 'text')
respond(req, res, responseCode, responseBody)

Mime types map:

  • text > 'text/plain'
  • html > 'text/html'
  • css > 'text/css'
  • js > 'application/javascript'
  • json > 'application/json'
  • es > 'application/ecmascript'
  • form > 'multipart/form-data'
  • url-form > 'application/x-www-form-urlencoded'
  • stream > 'application/octet-stream'
  • jpeg > 'image/jpeg'
  • png > 'image/png'

References

@lbzg/url-parse

2.0.8

6 years ago

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.23

6 years ago

1.0.22

6 years ago

1.0.21

6 years ago

1.0.20

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago