# autoserverw

> #### 快速开始

Latest version **1.0.13** (published 2020-04-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install autoserverw
pnpm add autoserverw
yarn add autoserverw
bun add autoserverw
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.13 |
| Published | 2020-04-20 |
| First published | 2018-06-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 14 |
| Unpacked size | 9.9 KB |
| Known vulnerabilities | 0 (+3 in 2 direct dependencies) |
| Install scripts | no |
| Maintainers | witt |

## Links

- npm: https://www.npmjs.com/package/autoserverw
- npm.io page: https://npm.io/package/autoserverw

## Dependencies (14)

- [cors](https://npm.io/package/cors.md) ^2.8.4
- [knex](https://npm.io/package/knex.md) ^0.14.6
- [path](https://npm.io/package/path.md) ^0.12.7
- [morgan](https://npm.io/package/morgan.md) ^1.9.0
- [autolib](https://npm.io/package/autolib.md) latest
- [express](https://npm.io/package/express.md) ^4.16.3
- [autohookw](https://npm.io/package/autohookw.md) latest
- [automodel](https://npm.io/package/automodel.md) latest
- [autorouterw](https://npm.io/package/autorouterw.md) latest
- [autoschemaw](https://npm.io/package/autoschemaw.md) latest
- [body-parser](https://npm.io/package/body-parser.md) ^1.18.3
- [compression](https://npm.io/package/compression.md) ^1.7.2
- [automiddleware](https://npm.io/package/automiddleware.md) latest
- [body-parser-xml](https://npm.io/package/body-parser-xml.md) ^1.1.0

## Recent versions

- 1.0.13 (latest) — 2020-04-20
- 1.0.12 — 2019-07-13
- 1.0.11 — 2018-10-06
- 1.0.10 — 2018-09-28
- 1.0.9 — 2018-08-27
- 1.0.8 — 2018-07-19
- 1.0.7 — 2018-07-18
- 1.0.6 — 2018-07-18
- 1.0.5 — 2018-07-18
- 1.0.4 — 2018-07-18
- 1.0.3 — 2018-07-18
- 1.0.2 — 2018-06-28
- 1.0.1 — 2018-06-26
- 1.0.0 — 2018-06-25

## README

### 快速搭建api访问

#### 快速开始

##### 1. 创建目录结构

```
- config
 |- test.json
- src
 |- hooks
   |- test
     |- index.js
 |- schema
   |- mongo
     |- test.js
 |- app.js
- package.json
```

- _config_: 下主要放置配置文件，具体文件名称将根据NODE_ENV来决定调用（如NODE_ENV=test，那么程序启动时将进行调用test.json）
- _src_: 项目文件存放位置
- _hooks_: 存放路由中间件 存放要求，与schema中定义的名称一致，程序将自动查找对应的中间价进行加载
- _schema_: 实体类存放地址，下面有使用mysql / mongo 目录进行区分实体对象所对应的库
- _app.js_: 启动文件

##### 2. 创建实体对象

> 这里我们使用mysql作为我们的数据库

**在src-schema-mysql 下创建文件 test.js**

```
'use strict';

var schema = {
  id: {build: (t) => t.increments('id').comment('id'), type: Number, primarykey: true},// primarykey 用于标注删除和查询时的对象属性
  name: {build: (t) => t.string('name'), type: String, notnull: true}, // notnull 用于标注该字段在接口post调用时为必填字段
  age: {build: (t)=> t.integer('age'), type: Number},
  cid: {build: (t) => t.string('cid').comment('身份证'), type: String, notnull: true, put: false, patch: false}, //put / patch: false 则表示当前属性将无法通过put和patch进行修改
  //使用mysql时，程序将自动追加createdat（创建时间）和updatedat，修改时间
}

module.exports = {
  schema,
  name: 'test',
  router: ['all']
}

```

> 在实体类中如果设置了router，并赋值，程序将自动创建对于的接口

| 属性   | 值                                    |
| :----: | :------------------------------------ |
| router | all, get, getByKey,put, patch, delete |

> 自动生成的接口格式为 /v1/[schema.name] (接口将在name后自动追加s，如已有s则不在追加，如实体对象test，name为test，则接口为 /v1/tests)


##### 3. 设置配置文件 config

```
{
  "port": 3000,
  "default": {
    "mysql": "mysqlapi"
  },
  "mysqlapi": {
    "client": "mysql",
    "connection": {
      "host": "127.0.0.1",
      "port": "3306",
      "user": "user",
      "password": "password",
      "database": "testapi"
    },
    "pool": {
      "min": 10,
      "max": 225
    }
  },
  "mysqls": ["mysqlapi"]
}
```

- _port_: 启动程序时的端口号
- _default_: 默认配置所对应的属性名
- _mysqlapi_: 具体mysql的相关配置，名称自定，需要在mysqls中正式启动
- _mysqls_: 配置的mysql配置参数进行启动与初始化


_实例_
_app.js_

```
'use strict';

// 快速启动
let app = require('autoserverw');
```

_package.json_ 修改scripts进行启动项目

```
{
  "scripts": {
    "test": "NODE_ENV=test node ./src/app.js"
  }
}
```

```
# 启动项目
$ npm run test
```

---
_Source: https://npm.io/package/autoserverw · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
