# API 说明

## $.run()

```js
$.run(port, [,settings])
$.run(8000)
```
配置参数并启动服务

### port
类型：`Number`  
本地监听的端口，可通过 `http://127.0.0.1 + port` 查看。  
例：[http://127.0.0.1:8000](http://127.0.0.1:8000)

```js
var $ = require('nodeajax')
$.run(8000)
/*
  .- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -.
  '                 The Web Develop Mock Server!                  '
  '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
  '                       _                   _                   '
  '   _   _              | |                 (_)                  '
  '  | \ | |   ___     __| |   ___    ____    _    ____  __  __   '
  '  |  \| |  / _ \   / _  |  / _ \  / _  |  | |  / _  | \ \/ /   '
  '  | |\  | | (_) | | (_| | |  __/ | (_| |  | | | (_| |  )  (    '
  '  |_| \_|  \___/   \____|  \___|  \____|  | |  \____| /_/\_\   '
  '                                         _/ |                  '
  '                                        |__/                   '
  '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
  Running at http://127.0.0.1:8000
*/
```
### settings

类型: `Object`
一个以 `{key: value}` 组成的配置信息。

@todo
例如

#### html

```js
['/demo/','/html/','/']
```
首页中 HTML list 模块读取的目录

**需要配图**

#### staticPath
类型：`String`  
```js
'/'
```
静态资源物理路径，相对于当前脚本运行的路径。有些后端架构中静态资源不是在根目录，而是类似 `/wwwroot/` 这种目录下。可以使用 `staticPath` 指定目录。

#### docPath
```js
{
    html: '/docs/data-api.html',
    markdown: '/docs/data-api.md'
}
```
markdown/html 文档的保存地址

#### settingsDir
类型：`String`  
```js
'/mock/'
```
模拟代码的存放位置

#### connect

等同于express 中的 connect，如不熟悉请无视。
类型：`Function`  
执行顺序在 settingsDir 中的模拟代码之前，建议使用此接口扩展功能

#### password 
类型：`String` `Boolean:false`  

参数是 false 则不需要密码

#### urlRewrite
类型：`Array`  

URL重定向,等同于 `String.replace(searchValue, replaceValue)` [深入理解JavaScript-replace](https://github.com/nimojs/blog/issues/2)

```js
[
    [
		// searchValue
		'/',
		// replaceValue
        '/html/index.html'
	]
]
```
如果传入参数是字符串会转换为正则，并加上 `^$` 
```js
// nodeajax src/route.js 源码
if (typeof searchValue === 'string') {
    // "/_console/" = "\/_console\/"
    searchValue = searchValue.replace(/([.$^{[(|)*+?\/\\])/g,'\\$1')
    searchValue = new RegExp('^' + searchValue + '$')
}
```
#### ajaxSettings

类型：`Object`

`$.ajax()` 默认参数

```js
{
        type: 'get',
        success: {
            status: 'success'
        },  
        error:{
            status: 'error',
            msg: 'Error detail'
        },
        doc: function() {/*!
{{title}}
---------
- url: [{{url}}]({{url}})
- type: `{{type}}`
##### Request:
{{data}}
##### Response:
{{#success}}
**success:**  
<pre>{{success}}</pre>
{{/success}}
{{#error}}
**error:**  
<pre>{{error}}</pre>
{{/error}}
        */},
        statusCode: '200|200',
        timeout: 300,
        dataType: 'html'
    }
```

##### type
类型：`String`

请求方式，允许是 `get` `post` `all` 。 `all` 表示同时接受所有类型请求。

> 请求方式还支持 `put` 和 `delete`

@todo
写上 以 `get` 方式向 `post` 定义的 ajax发送请求会出现的错误提示，并说明 `all` 可避免这种情况。

##### success
类型：`String|Array|Object` `Function:return data` `Function:express fn`

默认 Ajax 响应内容

##### error

类型：`String|Array|Object` `Function:return data` `Function:express fn`

可通过 cookie 控制切换的另一种 Ajax 响应内容

##### doc
类型：`String|Function|Object`
自动生成的 Ajax 文档模板(Handlebars)

```html
function() {/*!
{{title}}
---------
- url: [{{url}}]({{url}})
- type: `{{type}}`
#### Request:
{{data}}
#### Response:
{{#success}}
**success:**  
<pre>{{success}}</pre>
{{/success}}
{{#error}}
**error:**  
<pre>{{error}}</pre>
{{/error}}
*/}
```

##### statusCode

类型：`String|Array`

分别对应 [success](#success) 和 [error](#error) 两种响应内容的 HTTP Status Code 

```js
[200, 200]
```

##### timeout

类型：`String|Number`

延迟响应时间，当 node 接受到请求后延迟指定毫秒数再响应请求。用于模拟真实环境中的延迟响应。

```js
// 延迟 1000 毫秒响应结果
timeout: 1000
// 永远不响应
timeout: 'ever'
```

##### dataType

类型：`String`

Ajax响应的 HTTP Content-type

##### errorPage

类型：`String|Function`

404 响应显示的 html 页面