@levenx/koa-server
v1.0.9
Published
Koa 路由配置版
Downloads
5
Maintainers
Readme
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);