npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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()
    }
  }
}

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

| Project | NPM | | --------------------- | ------------------------------- | | http-assert | | | koa | | | koa-mount | | | koa-router | | | mongoose | | | node-schedule | | | sequelize | |

License

MIT