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

cno

v0.3.0

Published

Node.js Server Quick Develop Framework.

Downloads

5

Readme

CNO

NPM version NPM download NPM download npm Package Quality

GitHub watchers GitHub stars GitHub forks GitHub issues GitHub last commit (branch)

一个服务器快速开发框架。其中加入了Mysql客户端管理,Mongodb客户端管理,Redis客户端管理,Request网络请求功能。

This is a quick develop framework for web server.Including Mysql Client Manager,Mongodb Client Manager,Redis Client Manager,Network request kit,etc.

注意:请在ES6环境下运行。

Attention: Please make sure running in ES6.

更新日志     实例源码 / Sample

安装 Install

npm install -save cno

导入 Import

const CNO = require('cno');

创建实例 New Instance

const configFilePath = './example/config.js'
const configObject = require(configFilePath)
const cno = new CNO(configObject);

配置 Configure

// 添加配置
// config.api 配置接口
// config.api.duplicate // 是否支持重复声明接口
// config.api.list // 接口文件列表
// config.port 启动端口
// config.headers // 配置http默认的headers,例如跨域
// config.publicDir // 配置静态网页文件夹(绝对路径)
cno = cno.setConfig(require('./example/config.js'));
// 配置一个Express.js实例
cno = cno.setExpress(customExpressInstance);

添加插件 Add Plugin

// Request网络请求
cno = cno.usePlugin(CNO.Plugin.Request);
// Mysql客户端管理
cno = cno.usePlugin(CNO.Plugin.Mysql);
// Redis客户端管理
cno = cno.usePlugin(CNO.Plugin.Redis);
// Mongodb客户端管理
cno = cno.usePlugin(CNO.Plugin.Mongodb);

初始化 Initialize

// 初始化之后,cno实例无法在进行上述操作
cno = cno.initialize();

使用插件 Use Plugin

Plugin.Request

该插件是以request为基础进行封装的。

// 获取插件
cno = cno.usePlugin(CNO.Plugin.Request);
cno = cno.initialize();
request = cno.request;

// url 请求的url
// method 请求方法,cno.request.GET,cno.request.POST,cno.request.PUT,cno.request.DELETE
requestInstance = request.create(url,method);

// params 请求参数对象,eg:url='/api',params={a:1},request.url='/api?a=1'
requestInstance = requestInstance.setParams(params);
// data 请求body对象,eg:url='/api',data={a:1},request.payload={a:1}
requestInstance = request.create(url,cno.request.POST).setData(data);

// 'content-type': 'application/x-www-form-urlencoded'
requestInstance = request.create(url,cno.request.POST).setForm(data);
// 'content-type': 'multipart/form-data'
requestInstance = request.create(url,cno.request.POST).setFormData(data);

// headers 自定义headers
requestInstance = requestInstance.setHeaders(headers);

// auth 授权信息
requestInstance = requestInstance.setAuth(auth);
// oauth 授权信息
requestInstance = requestInstance.setOauth(oauth);

// 执行请求
// returnPromise 是否返回Promise对象,默认返回async/await
requestInstance.request (returnPromise);

// 获取相关教程
console.log(request.HELP());
co(function* () {
    const result = yield cno.request.create('https://img.shields.io/npm/v/cno.svg?style=flat-square', cno.request.GET).setParams({ a: 'a' }).request();
});

Plugin.Mysql

该插件是以mysql为基础进行封装的。

// 获取插件
cno = cno.usePlugin(CNO.Plugin.Mysql);
cno = cno.initialize();
mysql = cno.mysql;

// options 数据库配置
// options.host 主机
// options.subHost 备用主机
// options.port 端口
// options.user 用户名
// options.password 密码
// options.database 数据库
// options.maxThread 最大线程数,默认为 核心数 * 2 + 1,最少值为3
// options.multipleStatements 是否开启多行表达式,默认支持
mysqlConfig = new mysql.Config(options);
client = yield mysql.create(mysqlConfig);
    
// 执行sql
// client 由mysql.create创建的实例
// sql 合法的sql语句
// args 用于置换sql中的未知参数
result = yield mysql.exec(client, sql, args);
    
// 销毁client
mysql.destroy(client);

// 获取相关教程
console.log(mysql.HELP());
co(function *() {
    const config = new cno.mysql.Config({
        host,subHost,port,user,password,database,maxThread
    });
    const client = yield cno.mysql.create(config);
})

Plugin.Mongodb

该插件是以mongodb为基础进行封装的。

cno = cno.usePlugin(CNO.Plugin.Mongodb);
cno = cno.initialize();
mongodb = cno.mongodb;
co(function * () {
    // 单服务器
    origins = 'localhost:27017';
    // 服务器集群
    origins = ['localhost:27017', 'localhost:27018'];
    // options 请看官方教程
    manager = mongodb(origins, options);
    
    client = yield manager.getClient();
    
    // 获取数据库操作实例
    // dbName 数据库名字
    db = client.db(dbName);
    // 获取数据库实例
    // 实例相关操作请看官方教程
    dbInstance = db.instance;
    
    // 获取集合操作实例
    // name 集合名字
    collection = db.collection(name, options);
    // 获取集合实例
    // 实例相关操作请看官方教程
    collectionInstance = collection.instance;
    
    // 关闭客户端
    manager.close();
    
    // 获取相关教程
    console.log(mongodb.HELP());
});

Plugin.Redis

该插件是以redis为基础进行封装的。

// 获取插件
cno = cno.usePlugin(CNO.Plugin.Redis);
cno = cno.initialize();
redis = cno.redis;

// 创建实例
// host 主机
// port 端口
// password 密码
client = redis(host, port, password);

// 设置数据
// key 键
// value 值
// expire 有效时间(单位:秒),默认为86400(一天)
client = client.setData(key, value, expire);

// 获取数据
// key 键
value = yield client.getData(key);

// 删除数据
// key 键
client = client.removeData(key);

// 获取相关教程
console.log(redis.HELP());
const client = cno.redis(host, port, password);

关闭 ShutDown

// returnPromise 是否以Promise形式返回,默认async/await
const result = yield cno.shutDown(returnPromise);

添加接口 Add Api

在config.js中添加接口文件

module.exports = {
    api: {
        duplicate: false, // 不支持重复声明接口
        list: [require('./api/api1.js')]
    }
}

创建接口文件

mkdir api && cd api && vi api1.js

编辑接口文件

const CNO = require('cno');
const ApiBuilder = CNO.ApiBuilder;

// path支持RESTful语法
/* eg:
* add('/api1/:key',ApiBuilder.GET,(req)=>{
*   key = req.params.key
* })
* /

// ApiBuilder.create('/api/') 创建一个根路径为'/api/'的建造者
const api1 = ApiBuilder.create('/api/')
// 添加一个path为'/api1',方法为GET的接口
.add('/api1', ApiBuilder.GET, (req, res, next, cno) => {
    res.json({ route: api2.baseRoute });
})
// 添加一个path为'/api1/api2',方法为POST的接口
.add('/api1/api2', ApiBuilder.POST, (req, res, next, cno) => {
    res.json({ route: api2.baseRoute });
})
// 生成cno可用的接口信息
.build();

// ApiBuilder.create('/api/') 创建一个根路径为'/api/'的建造者
const api2 = ApiBuilder.create('/api/')
// 添加一个path为'/api2',方法为POST的接口
.add('/api2', ApiBuilder.POST, (req, res, next, cno) => {
    res.json({ route: api2.baseRoute });
})
// 生成cno可用的接口信息
.build();

// 只导出一个接口信息
// module.exports = api1;

// 同时导出两个接口信息
module.exports = { api1, api2 };
在执行cno.initialize方法后,config.js所注册的接口将会添加到httpServer中。

添加默认响应头

// 编辑config.js,添加headers字段
module.exports = {
    headers: [
        { 'Access-Control-Allow-Methods': 'POST, GET, PUT, DELETE, OPTIONS' },
        { 'Access-Control-Allow-Headers': 'X-Requested-With' },
        { 'Access-Control-Allow-Headers': 'Content-Type' },
        { 'Access-Control-Allow-Origin': 'https://chansos.com' },
        { 'Access-Control-Allow-Origin': 'https://www.chansos.com' }
    ]
};

自定义启动端口

// 编辑config.js,添加port字段
module.exports = {
    port: 3001 // 默认3000
};

更多内容

开源官网      实例源码      意见反馈      邮箱([email protected]) Official      Sample      Issues      Email([email protected])

打赏 Donate

支付宝 微信