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

@levenx/koa-server

v1.0.9

Published

Koa 路由配置版

Downloads

5

Readme

Koa 路由配置版

Koa 路由配置版

安装使用

npm i @levenx/koa-server

升级到最新版本

npm update @levenx/koa-server

功能简介

  • POST 请求体处理(包括图片上传)
  • JWT 鉴权
  • 系统日志
  • 跨域
  • mysql 集成
  • redis 集成
  • logs4j 日志
  • mail 集成
  • portal 默认首页

配置属性列表

| 参数 | 说明 | 备注 | | :------------- | :--------------- | :------------- | | appPrefix | 全局路由前缀 | | | routeDir | 路由文件配置 | | | | | | | jwt | jwt 鉴权属性 | | | jwt.secret | secret 密钥 | | | cors | 跨域配置 | | | | | | | mysql | mysql 配置 | | | mysql.modelDir | models 文件夹 | | | mysql.database | 数据库名 | | | mysql.host | 数据库 host | | | mysql.username | 数据库用户名 | | | mysql.password | 数据库密码 | | | | | | | redis | redis 配置 | | | redis.host | Redis host | | | redis.password | redis 密码 | | | redis.db | Redis db | | | redis.port | Redis 自定义端口 | | | redis.prefix | Redis key 前缀 | | | | | | | log | 日志配置 | true | object | | log.filename | 自定义日志文件名 | | | log.level | 日志输出级别 | | | | | | | | | mail | 邮件配置 | | | | mail.host | 发送host | | | | mail.auth.user | 发送邮箱用户名 | | | | mail.auth.pass | 授权码 | | | | mail.from | 自定义发送人 | | |

配置示例

{
  appPrefix: "/app",
  routeDir: path.resolve(__dirname, "./routes"),
  jwt: {
    secret: "xxxxxxx",
  },
  mysql: {
    modelDir: path.resolve(__dirname, "./models"),
    database: "koa-server",
    host: "127.0.0.1",
    username: "root",
    password: "xxxxxxx",
  },
  redis: {
    host: "127.0.0.1",
    password: "xxxxxxxx",
    db: "1",
    prefix:"my_"
  },
  log: { filename: "logs/levenx", level: "error" },
  mail: {
    host: "smtp.exmail.qq.com",
    auth: {
      user: "[email protected]",
      // 这里密码不是qq密码,是你设置的smtp授权码
      pass: "xxxxxxxx",
    },
    from: '"乐闻世界" <[email protected]>',
  },
  cors: true,
}

文件配置

//路由文件夹
// routes/common.js

const { home } = require("../services/CommonService");
const common = [
  {
    path: "/",
    service: home,
    unAuth: true,
  },
  {
    path: "/mail",
    service: mail,
    unAuth: true,
  },
];

module.exports = common;


//服务文件夹
// services/CommonService.js

module.exports = class CommonService {
  static async home(ctx, { mysql, redis, next }) {
  	//service 具有 mysql,redis 能力
    let home = await mysql.models.home.create({title:"levenx"});
    let xxx = await redis.getSync("xxx");
    return {
      code: 0,
      redis:xxx
    };
  }
  
   static async mail(ctx) {
    //发送邮件
    ctx.mail("[email protected]","koa保姆级集成服务","koa保姆级集成服务")
  }
};

//models文件夹
// models/home.js

const Sequelize = require("sequelize");
module.exports = {
  name: "home",
  tableName: "home",
  timestamps: true,
  freezeTableName: true,
  colums: {
    id: {
      field: "id",
      primaryKey: true,
      type: Sequelize.BIGINT,
      allowNull: false,
      autoIncrement: true,
    },
    title: {
      field: "title",
      type: Sequelize.STRING,
      allowNull: false,
    },
  },
};

项目启动

const Server = require("@levenx/koa-server");
const path = require("path");

const config = {
  appNmae: "levenx",
  appPrefix: "/app",
  routeDir: path.resolve(__dirname, "./routes"),
  jwt: {
    secret: "xxxxxxx",
  },
  mysql: {
    modelDir: path.resolve(__dirname, "./models"),
    database: "koa-server",
    host: "127.0.0.1",
    username: "root",
    password: "xxxxx",
  },
  redis: {
    host: "127.0.0.1",
    password: "xxxxx",
    db: "1",
  },
  log: { filename: "logs/levenx", level: "error" },
  mail: {
    host: "smtp.exmail.qq.com",
    auth: {
      user: "[email protected]",
      // 这里密码不是qq密码,是你设置的smtp授权码
      pass: "xxxx",
    },
    from: '"乐闻世界" <[email protected]>',
  },
  cors: true,
};

const App = new Server(config);

App.start(8000);

公众号

乐闻世界公众号