宇天 67e5da5098 init 2020-09-16 20:07:28 +08:00
lib init 2020-09-16 20:07:28 +08:00
.gitignore init 2020-09-16 20:07:28 +08:00
LICENSE init 2020-09-16 20:07:28 +08:00
Readme.md init 2020-09-16 20:07:28 +08:00
index.js init 2020-09-16 20:07:28 +08:00
package.json init 2020-09-16 20:07:28 +08:00

Readme.md

module info

http.request

http.request is a module that let you can easily using on http server.

Install

    npm i http.request

Usage

let Request = require('http.request')
let http = require('http')

http
  .createServer((req, res) => {
    let request = new Request(req, res)

    console.log(request.origin) // {req, res}

    // print the fixed url
    console.log(request.url)

    request.ip() // get client ip address

    // http://test.com/?foo=bar
    request.get('foo') // bar
  })
  .listen(3000)

API

origin

return the origin request object and response object.

console.log(request.origin) // {req: request, res: response}

app

return this first part of url

// abc.com/foo/bar
console.log(request.app) // foo

path

return this extra part of url

// abc.com/foo/bar/aa/bb
console.log(request.path) // ['bar', 'aa', 'bb']

url

return this fixed url

// abc.com/foo/bar/aa/bb
// abc.com////foo///bar/aa/bb
console.log(request.url) // foo/bar/aa/bb

router

return this router params

// abc.com/foo/bar/aa/bb/xx/yy
console.log(request.router) // {aa: 'bb', xx: 'yy'}

get([key[,xss]])

  • key <String> optional
  • xss <Boolean> optional

Get the fieldset from url. Just like PHP's $_GET[]; If xss is set to be true, the result will be filtered out with base xss.

// http://test.com?name=foo&age=18
request.get('name') // foo
request.get('age') // 18

// return all if not yet argument given
request.get() // {name: 'foo', age: 18}
request.get('weight') // return null if not exists

post([key[,xss]])

  • key <String> optional
  • xss <Boolean> optional

Get the http body content, just like PHP's $_POST[].

this function must use await/yiled command

// http://test.com
await request.post('name') // foo
await request.post('age') // 18

// return all if not yet argument given
await request.post() // {name: 'foo', age: 18}
await request.post('weight') // return null if not exists

header([key])

  • key <String> optional

return http headers.

request.header('user-agent') // Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ...

// return all if not yet argument given
request.header() // {'user-agent': '...'[, ...]}

ip()

return the client IP address.

It would return '127.0.0.1' maybe if in local area network.

对Http的request进一步封装, 提供常用的API
JavaScript 100%