xingweb
v1.0.6
Published
* 内置通用数据库连接,避免常见的数据库初始和引用的循环依赖错误问题 * 满足快速启动项目需要数据库的业务需求 * 统一路由入口的开发模式(按经验更好理解和管理) * 也可自定义路由
Downloads
6
Readme
web api框架
解决
- 内置通用数据库连接,避免常见的数据库初始和引用的循环依赖错误问题
- 满足快速启动项目需要数据库的业务需求
- 统一路由入口的开发模式(按经验更好理解和管理)
- 也可自定义路由
功能
- 快速新建api服务项目
- 内置常用数据库连接管理(目前支持mongo/redis)
- 内置统一入口路由,按type/action方式访问接口
- 按demo开发模式,可横向快速扩展业务模块
示例项目初始化
npm init -y
npm i xingweb
touch tsconfig.ts index.ts
mkdir demo_activity
touch demo_activity/index.ts
touch demo_activity/model.ts
mkdir demo_activity/routes
touch demo_activity/routes/test.ts
# 按下面示例代码补全后
npm run build
npm start
curl localhost:30000/run?type=demo&action=demo_find
入口文件 index.ts
import { DemoActivity } from './demo_activity';
const xingOption: XingOption = {
port: 30000,
database: {
mongodbs: {
"mongo1": { // mongo1是自定义的数据库key,可以添加多个数据库定义
uri: "mongodb://user:password@localhost:27017/db",
},
}
redises: {
"redis1": {
"port": 6379,
"host": <reids地址>,
"family": 4,
"password": <密码>,
"db": 0
}
}
}
activityClasses: [
DemoActivity
],
activityPath: '/run', // 自定义统一路由入口
routerDirs: [
// path.resolve(__dirname, "./routes")
]
}
const XingWeb = Xing.getInstance(xingOption)
XingWeb.run()
活动demo
demo_activity/index.ts
import { IActivity, Logger } from "xingweb"
import { DemoModel } from "./model"
class DemoActivity extends IActivity {
type: string = 'demo'
model = DemoModel()
constructor() {
super()
}
async operator(params: any): Promise<any> {
const { action } = params
switch (action) {
case "add":
return await this.add(params)
case "find":
return await this.find(params)
default:
break
}
return { error: "action not found" }
}
async find(parmms: any) {
const docs = await this.model.find().lean()
return docs
}
async add(params: { uid: number }) {
const { uid } = params
if (!uid) { return "require uid" }
const ret = await this.model.create({ uid })
return ret
}
}
export {
DemoActivity
}
demo_activity/model.ts
import mongoose from "mongoose"
import { XingMongoDb } from "xingweb"
const schema = new mongoose.Schema({
uid: Number,
}, {
versionKey: false,
timestamps: true
})
schema.index({ "uid": 1 }, { background: true })
const MODEL_NAME = "activity_xingweb_demo"
const generateModel = (serverKey: string = "mongo1") => {
return XingMongoDb.getModel(serverKey, MODEL_NAME, schema)
}
export {
generateModel as DemoModel
}
demo_activity/routes/test.ts
// 自定义路由示例
import { RouterCtx } from 'xingweb';
export default (router: any) => {
router.get('/test', async (ctx: RouterCtx) => {
ctx.body = "test:" + Date.now()
});
}
参考tsconfig.ts
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"allowJs": true,
"outDir": "output",
"baseUrl": "."
},
}