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

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": "."
    },
  }