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

tcb-server

v1.1.4

Published

Tencent cloudbase lite server framework

Downloads

6

Readme

tcb-server

NPM version npm download

腾讯云开发 轻量服务端辅助框架,旨在使用单云函数实例实现多个接口,管理云服务端逻辑,节省函数资源,减少项目中冷启动函数导致的体验下降。

使用方法

npm install --save tcb-server
// index.js
const TcbServer = require('tcb-server').Application;
const router = require('./router');
const app = new TcbServer({
  env: 'your env',
  credentials: {
    private_key_id: 'your private_key_id',
    private_key: 'your private_key',
    env_id: 'your env_id'
  }
});

exports.main = async (event) => {
  return app.serve({
    event,
    router
  });
};
// middleware/verify/auth.js
module.exports = async function (ctx, next) {
  try {
    const { userInfo } = await this.cloud.auth().getEndUserInfo();
    if (!userInfo.uid)
      throw new Error('用户未登录');
    await next();
  } catch (err) {
    ctx.body = {
      code: 401,
      message: err
    }
  }
}
// controller/v1/todo.js
// BaseContextClass基类构造了app,ctx,controller,service,cloud这些对象,辅助开发使用
const BaseContextClass = require('tcb-server').BaseContextClass;
class Todo extends BaseContextClass {
  async info () {
    const { ctx } = this;
    ctx.body = 'hello world!';
  }
}
module.exports = Todo;
// router.js
module.exports = app => {
  // controller, middleware, service目录下的js文件无需引用,可以直接在router中使用
  const { controller, middleware } = app;

  // 路由的使用方法参考[tcb-router](https://github.com/TencentCloudBase/tcb-router)
  app.router('/v1/todo/info', middleware.verify.auth, controller.v1.todo.info);
}

至此,一个最基本的多接口云函数结构就完成了

router的使用

router继承自tcb-router 但输入的$url变量名称改为了path,输出的_req名称改为request

BaseContextClass辅助对象

app

app作为根对象,挂载了所有其他对象,直接使用app来获取你要的对象也是可以的

ctx

ctx中挂载了用户的请求,具体结构为:

ctx.request = { body, raw: event, context, path: event.path }

controller

用于放置项目的输入输出代码,检查和整理用户的请求,接收service处理好的数据,按需求返回给用户

service

用于放置项目的逻辑代码,操作数据库与存储,处理数据

middleware

用于放置中间件,在事务流程中可以用来控制或清洗数据,完成后转至下一个路由环节

cloud

初始化过的@cloudbase/node-sdk 实例,可以直接操作云函数、云数据库、云存储 @cloudbase/node-sdk

最佳实例

目录结构

├── package.json
├── index.js
├── router.js
├── controller
│   └── v1
│       └── todo.js
├── service
│   └── v1
│       └── todo.js
├── middleware
│   └── auth.js
|
├── config
|   └── index.js

具体案例可以参考代码中的example目录