zyd-server-framework
v1.0.102
Published
基于nodejs,koa的server端框架
Downloads
112
Readme
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()
}
}
}
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
| Project | NPM | | --------------------- | ------------------------------- | | http-assert | | | koa | | | koa-mount | | | koa-router | | | mongoose | | | node-schedule | | | sequelize | |