1.0.13 • Published 4 years ago

autoserverw v1.0.13

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

快速搭建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,并赋值,程序将自动创建对于的接口

属性
routerall, 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
1.0.13

4 years ago

1.0.12

5 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