1.0.0
parent
67e5da5098
commit
84bfd6f227
50
Readme.md
50
Readme.md
|
@ -1,20 +1,19 @@
|
||||||
![module info](https://nodei.co/npm/http.request.png?downloads=true&downloadRank=true&stars=true)
|
![module info](https://nodei.co/npm/@gm5/request.png?downloads=true&downloadRank=true&stars=true)
|
||||||
|
|
||||||
# http.request
|
# @gm5/equest
|
||||||
|
> 对Http的request进一步封装, 提供常用的API.
|
||||||
> `http.request` is a module that let you can easily using on http server.
|
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm i http.request
|
npm i @gm5/request
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
let Request = require('http.request')
|
import Request from '@gm5/request'
|
||||||
let http = require('http')
|
import http from 'http'
|
||||||
|
|
||||||
http
|
http
|
||||||
.createServer((req, res) => {
|
.createServer((req, res) => {
|
||||||
|
@ -36,7 +35,7 @@ http
|
||||||
## API
|
## API
|
||||||
|
|
||||||
### origin
|
### origin
|
||||||
> return the origin request object and response object.
|
> 返回原始的response & request对象
|
||||||
|
|
||||||
```js
|
```js
|
||||||
console.log(request.origin) // {req: request, res: response}
|
console.log(request.origin) // {req: request, res: response}
|
||||||
|
@ -44,7 +43,7 @@ console.log(request.origin) // {req: request, res: response}
|
||||||
|
|
||||||
|
|
||||||
### app
|
### app
|
||||||
> return this first part of url
|
> 返回一级路由的名字
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// abc.com/foo/bar
|
// abc.com/foo/bar
|
||||||
|
@ -53,7 +52,7 @@ console.log(request.app) // foo
|
||||||
|
|
||||||
|
|
||||||
### path
|
### path
|
||||||
> return this extra part of url
|
> 以数组形式,返回除一级路由之外剩下的路径
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// abc.com/foo/bar/aa/bb
|
// abc.com/foo/bar/aa/bb
|
||||||
|
@ -61,7 +60,7 @@ console.log(request.path) // ['bar', 'aa', 'bb']
|
||||||
```
|
```
|
||||||
|
|
||||||
### url
|
### url
|
||||||
> return this fixed url
|
> 返回修正过的url路径
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// abc.com/foo/bar/aa/bb
|
// abc.com/foo/bar/aa/bb
|
||||||
|
@ -69,30 +68,21 @@ console.log(request.path) // ['bar', 'aa', 'bb']
|
||||||
console.log(request.url) // foo/bar/aa/bb
|
console.log(request.url) // foo/bar/aa/bb
|
||||||
```
|
```
|
||||||
|
|
||||||
### router
|
|
||||||
> return this router params
|
|
||||||
|
|
||||||
```js
|
|
||||||
// abc.com/foo/bar/aa/bb/xx/yy
|
|
||||||
console.log(request.router) // {aa: 'bb', xx: 'yy'}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### get([key[,xss]])
|
### get([key[,xss]])
|
||||||
|
|
||||||
* key `<String>` optional
|
* key `<String>` 字段名 [可选], 不则返回全部参数
|
||||||
* xss `<Boolean>` optional
|
* xss `<Boolean>` 是否进行xss过滤 [可选], 默认为ture
|
||||||
|
|
||||||
|
> 返回URL上的query参数, 类似于`$_GET[]`;
|
||||||
|
|
||||||
> 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.
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// http://test.com?name=foo&age=18
|
// http://test.com?name=foo&age=18
|
||||||
request.get('name') // foo
|
request.get('name') // foo
|
||||||
request.get('age') // 18
|
request.get('age') // 18
|
||||||
|
|
||||||
// return all if not yet argument given
|
|
||||||
request.get() // {name: 'foo', age: 18}
|
request.get() // {name: 'foo', age: 18}
|
||||||
request.get('weight') // return null if not exists
|
request.get('weight') // return null if not exists
|
||||||
```
|
```
|
||||||
|
@ -102,9 +92,9 @@ request.get('weight') // return null if not exists
|
||||||
* key `<String>` optional
|
* key `<String>` optional
|
||||||
* xss `<Boolean>` optional
|
* xss `<Boolean>` optional
|
||||||
|
|
||||||
> Get the http body content, just like PHP's `$_POST[]`.
|
> 读取post请求的body, 类似于 `$_POST[]`.
|
||||||
>
|
|
||||||
> **this function must use await/yiled command**
|
> **该方法返回的是Promise对象**
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// http://test.com
|
// http://test.com
|
||||||
|
@ -118,9 +108,9 @@ await request.post('weight') // return null if not exists
|
||||||
|
|
||||||
### header([key])
|
### header([key])
|
||||||
|
|
||||||
* key `<String>` optional
|
* key `<String>` 字段名[可选], 不传则返回全部
|
||||||
|
|
||||||
> return http headers.
|
> 返回请求头
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
request.header('user-agent') // Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ...
|
request.header('user-agent') // Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ...
|
||||||
|
@ -131,6 +121,6 @@ request.header() // {'user-agent': '...'[, ...]}
|
||||||
|
|
||||||
### ip()
|
### ip()
|
||||||
|
|
||||||
> return the client IP address.
|
> 获取客户端IP地址.
|
||||||
>
|
>
|
||||||
> It would return '127.0.0.1' maybe if in local area network.
|
> It would return '127.0.0.1' maybe if in local area network.
|
||||||
|
|
Loading…
Reference in New Issue