Class: HttpServer

HttpServer

new HttpServer(options)

创建一个http服务程序,包括https和http服务
This:
Parameters:
Name Type Description
options Object
Source:
Example
'use strict';

var HttpServer = require('epi').HttpServer;

var util = require('util');

var config = {
     host : null,
     key : 'string',    //https监听时需要这个key
     cert : 'string',   //https监听的时候需要证书
     version : '1.0.0'  //api默认的版本信息
     port : {
         http  : 8000, //如果想要监听http端口,直接写
         https : 4433  //如果不想监听https,直接置为null
     }
}

//创建一个httpServer=====================================
var httpServer = new HttpServer(config);


//设置==================================================
httpServer.setting(function (app) {
   app.set('trust proxy', true);
   app.set('x-powered-by', false);
});
//导入中间件=============================================
var body = require('body-parser');
var cookie = require('cookie-parser');
var compression = require("compression");

 //应用中间件
httpServer.use(function (app) {
   app.use(compression());
   app.use(cookie());
   app.use(body.json());
   app.use(body.urlencoded({
       extended: true
   }));
});


//导入路由==============================================
var testRouter = require('./router/test');

//注册路由
testRouter.map(httpServer);
//错误处理=============================================
httpServer.error(function (err, req, res, next) {
   console.error(err.stack);
   res.status(500).json({
       error: err
   });
});

Methods

all(path, options, options, handler, ttl)

Router.all 相当于express router.all
Parameters:
Name Type Description
path String | Regex '/api/name' | /^\/api\/name$/
options String .v '1.0.1'
options String .c 你定义的频道类型
handler function function(req, res, next){}
ttl Number 设置api缓存时间
Source:
Example
使用缓存:
httpServer.all('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, 100)

不使用
httpServer.all('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){})
httpServer.all('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, null)

cluster(number, callback)

以集群的方式启动程序
Parameters:
Name Type Description
number Number 要启动的监听进程数量,null || 0 启动和 cpu个数 - 1 的进程数量
callback function 监听后返回的消息
Source:
Example
//子进程启动事件
 httpServer.on('childStart', function(worker){
      //记录子进程启动
      logger.info(Date.now(), worker.pid);
 });


 //子进程退出事件
 httpServer.on('childStop', function(worker, code, signal){
     //记录子进程非正常退出
     if(!signal && code !== 0){
         logger.fatal(Date.now(), worker.pid, code, signal);
     }
 });

 //子进程退出事件
 httpServer.on('childRestart', function(worker){
     //记录子进程重启
     logger.fatal(Date.now(), worker.pid);
 });


 //使用集群方式监听=======================================
 httpServer.cluster(0, function (message) {
     console.log(message);
 });

delete(path, options, options, handler, ttl)

Router.delete 相当于express router.delete
Parameters:
Name Type Description
path String | Regex '/api/name' | /^\/api\/name$/
options String .v '1.0.1'
options String .c 你定义的频道类型
handler function function(req, res, next){}
ttl Number 设置api缓存时间
Source:
Example
使用缓存:
httpServer.delete('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, 100)

不使用
httpServer.delete('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){})
httpServer.delete('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, null)

error(fn)

添加错误处理
Parameters:
Name Type Description
fn function function(err, req, res, next){}
Source:

get(path, options, options, handler, ttl)

Router.get 相当于express router.get
Parameters:
Name Type Description
path String | Regex '/api/name' | /^\/api\/name$/
options String .v '1.0.1'
options String .c 你定义的频道类型
handler function function(req, res, next){}
ttl Number 设置api缓存时间
Source:
Example
使用缓存:
httpServer.get('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, 100)

不使用
httpServer.get('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){})
httpServer.get('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, null)
Router.header 相当于express router.header
Parameters:
Name Type Description
path String | Regex '/api/name' | /^\/api\/name$/
options String .v '1.0.1'
options String .c 你定义的频道类型
handler function function(req, res, next){}
ttl Number 设置api缓存时间
Source:
Example
使用缓存:
httpServer.header('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, 100)

不使用
httpServer.header('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){})
httpServer.header('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, null)

listen(callback)

监听端口
Parameters:
Name Type Description
callback function 监听后返回的消息
Source:
Example
httpServer.listen(8000, function(err, message){
     console.log(message);
})

options(path, options, options, handler, ttl)

Router.options 相当于express router.options
Parameters:
Name Type Description
path String | Regex '/api/name' | /^\/api\/name$/
options String .v '1.0.1'
options String .c 你定义的频道类型
handler function function(req, res, next){}
ttl Number 设置api缓存时间
Source:
Example
使用缓存:
httpServer.options('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, 100)

不使用
httpServer.options('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){})
httpServer.options('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, null)

post(path, options, options, handler, ttl)

Router.post 相当于express router.post
Parameters:
Name Type Description
path String | Regex '/api/name' | /^\/api\/name$/
options String .v '1.0.1'
options String .c 你定义的频道类型
handler function function(req, res, next){}
ttl Number 设置api缓存时间
Source:
Example
使用缓存:
httpServer.post('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, 100)

不使用
httpServer.post('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){})
httpServer.post('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, null)

put(path, options, options, handler, ttl)

Router.put 相当于express router.put
Parameters:
Name Type Description
path String | Regex '/api/name' | /^\/api\/name$/
options String .v '1.0.1'
options String .c 你定义的频道类型
handler function function(req, res, next){}
ttl Number 设置api缓存时间
Source:
Example
使用缓存:
httpServer.put('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, 100)

不使用
httpServer.put('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){})
httpServer.put('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, null)

route(fn)

注册路由
Parameters:
Name Type Description
fn function function(app){}
Source:

setting(fn)

设置程序属性
Parameters:
Name Type Description
fn function function(app){}
Source:
Example
httpServer.setting(function (app) {
  app.set('trust proxy', true);
  app.set('x-powered-by', false);
});

trace(path, options, options, handler, ttl)

Router.trace 相当于express router.trace
Parameters:
Name Type Description
path String | Regex '/api/name' | /^\/api\/name$/
options String .v '1.0.1'
options String .c 你定义的频道类型
handler function function(req, res, next){}
ttl Number 设置api缓存时间
Source:
Example
使用缓存:
httpServer.trace('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, 100)

不使用
httpServer.trace('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){})
httpServer.trace('/test', {v : '1.0.0', c : 'pc'}, function(req, res, next){}, null)

use(fn)

应用中间件
Parameters:
Name Type Description
fn function function(app){}
Source:
Example
httpServer.use(function(app){
     app.use(compression());
     app.use(timeout('10s'));
     app.use(cookie());
     app.use(body.json());
});