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

rest-express

v1.2.0

Published

A rest api framework based on express.

Downloads

7

Readme

rest-express Google Analytics

A rest api framework based on express.

NPM Version npm npm Build Status Coverage Status Dependency Status License

How to use?

$ npm install rest-express --save

let restExpress = require('rest-express');
restExpress.startServer(options)
  .then((server) => {
    let address = server.address();
    console.log('Server started', address);
  }, (err) => {
    console.error(err);
  });

About Options

let options = {
  port: 3000, //必须提供,服务端口
  enableResponseTime: true, //是否启用响应时间记录(会在header中,设置X-Response-Time的值,单位毫秒)
  responseTimeOptions: { //responseTime的参数配置,请参考:https://github.com/expressjs/response-time
    digits: 4, //default 3
    header: 'use-time',
    suffix: false
  },
  enableLog: true, //是否启用日志
  logFormat: 'combined', //设置日志格式
  logOptions: { //日志插件相关参数,请参考:https://github.com/expressjs/morgan

  },
  enableCors: true, //是否启用Cors支持
  corsOptions: { //Cors参数,请参考:https://github.com/expressjs/cors

  },
  urlParserOptions: {}, //转换url参数配置,请参考:https://github.com/expressjs/body-parser
  jsonParserOptions: {}, //转换json参数配置,请参考:https://github.com/expressjs/body-parser
  enableGzip: true, //是否启用Gzip支持
  gzipOptions: {}, //gzip配置,请参考:https://github.com/expressjs/compression
  enableHttps: false, //是否启用Https
  httpsOptions: { //Https配置项,提供{key: '', cert: ''}
    key: '',
    cert: ''
  },
  onRoutesLoading: (app) => { //加载路由之前要执行的操作,参数app = express();
    console.log('before load routes');
  },
  onRoutesLoaded: (app) => { //加载路由之后要执行的操作,参数app = express();
    console.log('after load routes');
  },
  routesPath: path.join(__dirname, 'routes'), //路由目录,所有要加载的路由都放置在此处。
  apiPrefix: '/' //全局Api前缀, 如: /api/v1
};

注意:其中 portroutesPath 为必填参数。

How to write route?

'use strict';

let router = new Router();
let testBiz = require('./../bizs/testBiz');

router.get('/test', testBiz.test);

//每个路由文件需要如下方式导出:
module.exports = {
  priority: 0, // 可选参数,默认0,优先级,越大越先加载
  router: router, // 必须参数,router对象。
  prefix: '/test/abc' // 可选参数,默认/,router前缀,会拼接在options.apiPrefix之后。
};

How to validate object?

rest-experss 集成了 Joi 验证库,通过提供了一个全局的 Validator 对象,如果我们要验证,只需要如下调用:

Validator.validate(req.body, schema [,options])
  .then(() => {
    // do some logic
  })
  .catch(next);

其中 options 的默认值为: { allowUnknown: true, abortEarly: false },可自行指定参数,参考:Joi Api Docs

How to use MSSQL?

let MSSQL = require('rest-express').MSSQL;

let config = {
  user: 'xxx', // UserName
  password: 'xxxx', // Password
  server: '127.0.0.1', // Server Name(IP)
  port: 1433, // Data Server Port 
  database: 'XXX', // Data base name
  pool: { //线程池
    max: 50, // 最大线程数
    min: 0, // 最小线程数
    idleTimeoutMillis: 30 * 1000 // 超时时间
  }
};
let db = new MSSQL(config);

db.executeNonQuery('update JayTestTable1 set password = @pwd', {pwd: '中文'})
  .then((count) => {
    console.log(count); //受影响的行数
  });
db.executeScalar('select top 1 * from JayTestTable1 where id = 15')
  .then((data) => {
    console.log(data); //查询结果的第一行数据(建议查询语句只会有第一行数据)
    //如果查询结果是一个简单数据,如查询count,那么data则直接是这个count值,不在是一个对象。
  });
db.executeQuery('select * from JayTestTable1')
  .then((rows) => {
    console.log(rows); //数据集
  });
db.executeProcedure('up_GetPersonalApiList', {userName: 'abc'}, {}) //存储过程名称、输入参数、输出参数
  .then((result) => {
    console.log(result.result); //执行结果
    console.log(result.affectedRowCount); //受影响的行数
    console.log(result.resultOutput); // 输出参数
  });