1.0.102 • Published 11 months ago

zyd-server-framework v1.0.102

Weekly downloads
207
License
MIT
Repository
-
Last release
11 months ago

zyd-server-framework

Installation

$ npm install -s zyd-server-framework

Quickstart

/index.js

const Zsf = require("zyd-server-framework")
const app = new Zsf() 
app.start()

Options

/index.js

const Zsf = require("zyd-server-framework")
const app = new Zsf({ 
  baseUrl: "/api", // 基础路径设置
  beforeInit(koaApp){ // 生命周期函数 - 初始化前
    koaApp.use(require("koa2-cors")()) // 跨域设置
    koaApp.use(require("koa-bodyparser")()) // body设置
    const session = require("koa-session") // session设置
    koaApp.keys = ["some secret hurr"]
    koaApp.use(session({
      key: "koa:sess",
      maxAge: 86400000,
      overwrite: true,
      httpOnly: true,
      signed: true,
      rolling: false,
      renew: false,
    }, koaApp))
  },
  afterInit(koaApp){ ... } // 生命周期函数 - 初始化后
})
app.start(3000, callBack(){
  console.log("start on port:3000")
})

config

/config/conf.js

module.exports = {
  db: [
    /* type: 'mongo' | 'mysql' | 'mariadb' | 'postgres' | 'mssql' 其一 */
    {
      type:"mongo", 
      name:"mongo",
      options: {
          connect:"user:password@localhost:27017",
          dbName: "db",
      }
    },
    {
      type:"mysql",
      name:"mysql1",
      options: {
        dialect: "mysql",
        host: "localhost",
        database: "database",
        username: "root",
        password: "example"
      }
    },
    {
      type:"mssql",
      name:"mssql1",
      options: {
        dialect: "mssql",
        host: "localhost",
        database: "database",
        username: "sa",
        password: ""
      }
    }
  ],
  middleware: [ // 中间件
    "error",
    "favicon",
  ]
}
app.$config.conf.db
app.$config.conf.middleware

controller

/controller/home.js

module.exports = class Test {
  constructor(app) {
    this["get /"] = async () => {
      return "Hello World"
    }
    this["get /user"] = async () => {
      return await app.$service.user.getName()
    }
  }
}

http://localhost:3000/home

service

/service/user.js

const delay = (data, tick) => new Promise(resolve => {
  setTimeout(() => {
    resolve(data)
  }, tick);
})

module.exports = class User {
  constructor(app) {
    this.app = app
  }
  async getName () {
    return delay("zyd", 1000)
  }
}
app.$service.user.getName()

middleware

/middleware/favicon.js

module.exports = async (ctx, next) => {
  if (ctx.path === "/favicon.ico") {
    ctx.body = ""
    return
  }
  await next()
}

/middleware/callBack.js

const Router = require('koa-router')
const router = new Router();
const assert = require("http-assert")
        
module.exports = (router.get("/callBack/:id", async ctx => {
  const id = ctx.params.id
  assert(id, 400, "缺少id")
  ctx.body = "中间件前置路由"
})).routes()

http://localhost:3000/callBack/1234567

/middleware/homePage.js

// 静态页面中间件
const static = require("koa-static")
const mount = require('koa-mount')
module.exports = app => mount('/homePage', static('./homePage'))

/homePage/index.html

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>homePage</title>
</head>
<body>
  <h3>zyd-server-framework</h3>
  <p><a href="https://www.npmjs.com/package/zyd-server-framework">https://www.npmjs.com/package/zyd-server-framework</a></p>
  <p><a href="https://github.com/hfzhae/zyd-server-framework">https://github.com/hfzhae/zyd-server-framework</a></p>
</body>
</html>

http://localhost:3000/homePage

model

/model/user.js

const mongoose = require("mongoose")
const schema = new mongoose.Schema({
  userName: { type: String },
  age: { type: Number }
})
module.exports = app => app.$config.db.mongo.model("user", schema)
app.$model.user

plugin

/plugin/utils.js

module.exports = app => ({
  timestamp() {
    return parseInt(Date.parse(new Date) / 1000)
  },
})
app.$plugin.utils.timestamp()

schedule

/schedule/index.js

module.exports = class Index {
  constructor(app){
    this.interval = "* * * 1 * *" //crontab格式
    this.handler = () => {
      console.log("这是一个定时任务 " + new Date().toLocaleString())
    }
  }
}

dependencies

ProjectNPM
http-assert
koa
koa-mount
koa-router
mongoose
node-schedule
sequelize

License

MIT

1.0.102

11 months ago

1.0.101

1 year ago

1.0.88

1 year ago

1.0.87

1 year ago

1.0.86

1 year ago

1.0.89

1 year ago

1.0.91

1 year ago

1.0.90

1 year ago

1.0.84

2 years ago

1.0.83

2 years ago

1.0.82

2 years ago

1.0.81

2 years ago

1.0.85

2 years ago

1.0.80

2 years ago

1.0.79

2 years ago

1.0.78

2 years ago

1.0.77

3 years ago

1.0.76

3 years ago

1.0.75

3 years ago

1.0.74

3 years ago

1.0.73

3 years ago

1.0.72

3 years ago

1.0.71

3 years ago

1.0.70

3 years ago

1.0.69

3 years ago

1.0.66

3 years ago

1.0.68

3 years ago

1.0.67

3 years ago

1.0.62

3 years ago

1.0.61

3 years ago

1.0.65

3 years ago

1.0.64

3 years ago

1.0.63

3 years ago

1.0.60

3 years ago

1.0.59

3 years ago

1.0.58

3 years ago

1.0.57

3 years ago

1.0.56

3 years ago

1.0.49

3 years ago

1.0.51

3 years ago

1.0.50

3 years ago

1.0.54

3 years ago

1.0.53

3 years ago

1.0.52

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.40

3 years ago

1.0.44

3 years ago

1.0.43

3 years ago

1.0.42

3 years ago

1.0.41

3 years ago

1.0.48

3 years ago

1.0.47

3 years ago

1.0.46

3 years ago

1.0.45

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago