UNPKG

epi

Version:

A Api Service Framework Base On Express.js

3,030 lines (855 loc) 38.1 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Class: HttpServer</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Class: HttpServer</h1> <section> <header> <h2> HttpServer </h2> </header> <article> <div class="container-overview"> <h4 class="name" id="HttpServer"><span class="type-signature"></span>new HttpServer<span class="signature">(options)</span><span class="type-signature"></span></h4> <div class="description"> 创建一个http服务程序,包括https和http服务 </div> <h5>This:</h5> <ul><li>{<a href="HttpServer.html">HttpServer</a>}</li></ul> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">Object</span> </td> <td class="description last"></td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line20">line 20</a> </li></ul></dd> </dl> <h5>Example</h5> <pre class="prettyprint"><code>'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 }); });</code></pre> </div> <h3 class="subsection-title">Methods</h3> <h4 class="name" id="all"><span class="type-signature"></span>all<span class="signature">(path, options, options, handler, ttl)</span><span class="type-signature"></span></h4> <div class="description"> Router.all 相当于express router.all </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>path</code></td> <td class="type"> <span class="param-type">String</span> | <span class="param-type">Regex</span> </td> <td class="description last">'/api/name' | /^\/api\/name$/</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.v '1.0.1'</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.c 你定义的频道类型</td> </tr> <tr> <td class="name"><code>handler</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">function(req, res, next){}</td> </tr> <tr> <td class="name"><code>ttl</code></td> <td class="type"> <span class="param-type">Number</span> </td> <td class="description last">设置api缓存时间</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line237">line 237</a> </li></ul></dd> </dl> <h5>Example</h5> <pre class="prettyprint"><code>使用缓存: 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)</code></pre> <h4 class="name" id="cluster"><span class="type-signature"></span>cluster<span class="signature">(number, callback)</span><span class="type-signature"></span></h4> <div class="description"> 以集群的方式启动程序 </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>number</code></td> <td class="type"> <span class="param-type">Number</span> </td> <td class="description last">要启动的监听进程数量,null || 0 启动和 cpu个数 - 1 的进程数量</td> </tr> <tr> <td class="name"><code>callback</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">监听后返回的消息</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line628">line 628</a> </li></ul></dd> </dl> <h5>Example</h5> <pre class="prettyprint"><code>//子进程启动事件 httpServer.on('childStart', function(worker){ //记录子进程启动 logger.info(Date.now(), worker.pid); }); //子进程退出事件 httpServer.on('childStop', function(worker, code, signal){ //记录子进程非正常退出 if(!signal &amp;&amp; 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); });</code></pre> <h4 class="name" id="delete"><span class="type-signature"></span>delete<span class="signature">(path, options, options, handler, ttl)</span><span class="type-signature"></span></h4> <div class="description"> Router.delete 相当于express router.delete </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>path</code></td> <td class="type"> <span class="param-type">String</span> | <span class="param-type">Regex</span> </td> <td class="description last">'/api/name' | /^\/api\/name$/</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.v '1.0.1'</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.c 你定义的频道类型</td> </tr> <tr> <td class="name"><code>handler</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">function(req, res, next){}</td> </tr> <tr> <td class="name"><code>ttl</code></td> <td class="type"> <span class="param-type">Number</span> </td> <td class="description last">设置api缓存时间</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line355">line 355</a> </li></ul></dd> </dl> <h5>Example</h5> <pre class="prettyprint"><code>使用缓存: 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)</code></pre> <h4 class="name" id="error"><span class="type-signature"></span>error<span class="signature">(fn)</span><span class="type-signature"></span></h4> <div class="description"> 添加错误处理 </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>fn</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">function(err, req, res, next){}</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line498">line 498</a> </li></ul></dd> </dl> <h4 class="name" id="get"><span class="type-signature"></span>get<span class="signature">(path, options, options, handler, ttl)</span><span class="type-signature"></span></h4> <div class="description"> Router.get 相当于express router.get </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>path</code></td> <td class="type"> <span class="param-type">String</span> | <span class="param-type">Regex</span> </td> <td class="description last">'/api/name' | /^\/api\/name$/</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.v '1.0.1'</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.c 你定义的频道类型</td> </tr> <tr> <td class="name"><code>handler</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">function(req, res, next){}</td> </tr> <tr> <td class="name"><code>ttl</code></td> <td class="type"> <span class="param-type">Number</span> </td> <td class="description last">设置api缓存时间</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line266">line 266</a> </li></ul></dd> </dl> <h5>Example</h5> <pre class="prettyprint"><code>使用缓存: 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)</code></pre> <h4 class="name" id="header"><span class="type-signature"></span>header<span class="signature">(path, options, options, handler, ttl)</span><span class="type-signature"></span></h4> <div class="description"> Router.header 相当于express router.header </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>path</code></td> <td class="type"> <span class="param-type">String</span> | <span class="param-type">Regex</span> </td> <td class="description last">'/api/name' | /^\/api\/name$/</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.v '1.0.1'</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.c 你定义的频道类型</td> </tr> <tr> <td class="name"><code>handler</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">function(req, res, next){}</td> </tr> <tr> <td class="name"><code>ttl</code></td> <td class="type"> <span class="param-type">Number</span> </td> <td class="description last">设置api缓存时间</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line384">line 384</a> </li></ul></dd> </dl> <h5>Example</h5> <pre class="prettyprint"><code>使用缓存: 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)</code></pre> <h4 class="name" id="listen"><span class="type-signature"></span>listen<span class="signature">(callback)</span><span class="type-signature"></span></h4> <div class="description"> 监听端口 </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>callback</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">监听后返回的消息</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line513">line 513</a> </li></ul></dd> </dl> <h5>Example</h5> <pre class="prettyprint"><code>httpServer.listen(8000, function(err, message){ console.log(message); })</code></pre> <h4 class="name" id="options"><span class="type-signature"></span>options<span class="signature">(path, options, options, handler, ttl)</span><span class="type-signature"></span></h4> <div class="description"> Router.options 相当于express router.options </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>path</code></td> <td class="type"> <span class="param-type">String</span> | <span class="param-type">Regex</span> </td> <td class="description last">'/api/name' | /^\/api\/name$/</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.v '1.0.1'</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.c 你定义的频道类型</td> </tr> <tr> <td class="name"><code>handler</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">function(req, res, next){}</td> </tr> <tr> <td class="name"><code>ttl</code></td> <td class="type"> <span class="param-type">Number</span> </td> <td class="description last">设置api缓存时间</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line413">line 413</a> </li></ul></dd> </dl> <h5>Example</h5> <pre class="prettyprint"><code>使用缓存: 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)</code></pre> <h4 class="name" id="post"><span class="type-signature"></span>post<span class="signature">(path, options, options, handler, ttl)</span><span class="type-signature"></span></h4> <div class="description"> Router.post 相当于express router.post </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>path</code></td> <td class="type"> <span class="param-type">String</span> | <span class="param-type">Regex</span> </td> <td class="description last">'/api/name' | /^\/api\/name$/</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.v '1.0.1'</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.c 你定义的频道类型</td> </tr> <tr> <td class="name"><code>handler</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">function(req, res, next){}</td> </tr> <tr> <td class="name"><code>ttl</code></td> <td class="type"> <span class="param-type">Number</span> </td> <td class="description last">设置api缓存时间</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line297">line 297</a> </li></ul></dd> </dl> <h5>Example</h5> <pre class="prettyprint"><code>使用缓存: 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)</code></pre> <h4 class="name" id="put"><span class="type-signature"></span>put<span class="signature">(path, options, options, handler, ttl)</span><span class="type-signature"></span></h4> <div class="description"> Router.put 相当于express router.put </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>path</code></td> <td class="type"> <span class="param-type">String</span> | <span class="param-type">Regex</span> </td> <td class="description last">'/api/name' | /^\/api\/name$/</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.v '1.0.1'</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.c 你定义的频道类型</td> </tr> <tr> <td class="name"><code>handler</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">function(req, res, next){}</td> </tr> <tr> <td class="name"><code>ttl</code></td> <td class="type"> <span class="param-type">Number</span> </td> <td class="description last">设置api缓存时间</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line326">line 326</a> </li></ul></dd> </dl> <h5>Example</h5> <pre class="prettyprint"><code>使用缓存: 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)</code></pre> <h4 class="name" id="route"><span class="type-signature"></span>route<span class="signature">(fn)</span><span class="type-signature"></span></h4> <div class="description"> 注册路由 </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>fn</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">function(app){}</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line489">line 489</a> </li></ul></dd> </dl> <h4 class="name" id="setting"><span class="type-signature"></span>setting<span class="signature">(fn)</span><span class="type-signature"></span></h4> <div class="description"> 设置程序属性 </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>fn</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">function(app){}</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line465">line 465</a> </li></ul></dd> </dl> <h5>Example</h5> <pre class="prettyprint"><code>httpServer.setting(function (app) { app.set('trust proxy', true); app.set('x-powered-by', false); });</code></pre> <h4 class="name" id="trace"><span class="type-signature"></span>trace<span class="signature">(path, options, options, handler, ttl)</span><span class="type-signature"></span></h4> <div class="description"> Router.trace 相当于express router.trace </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>path</code></td> <td class="type"> <span class="param-type">String</span> | <span class="param-type">Regex</span> </td> <td class="description last">'/api/name' | /^\/api\/name$/</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.v '1.0.1'</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">.c 你定义的频道类型</td> </tr> <tr> <td class="name"><code>handler</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">function(req, res, next){}</td> </tr> <tr> <td class="name"><code>ttl</code></td> <td class="type"> <span class="param-type">Number</span> </td> <td class="description last">设置api缓存时间</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line442">line 442</a> </li></ul></dd> </dl> <h5>Example</h5> <pre class="prettyprint"><code>使用缓存: 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)</code></pre> <h4 class="name" id="use"><span class="type-signature"></span>use<span class="signature">(fn)</span><span class="type-signature"></span></h4> <div class="description"> 应用中间件 </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>fn</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">function(app){}</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="HttpServer.js.html">HttpServer.js</a>, <a href="HttpServer.js.html#line480">line 480</a> </li></ul></dd> </dl> <h5>Example</h5> <pre class="prettyprint"><code>httpServer.use(function(app){ app.use(compression()); app.use(timeout('10s')); app.use(cookie()); app.use(body.json()); });</code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="HttpServer.html">HttpServer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#app">app</a></li><li><a href="global.html#https">https</a></li><li><a href="global.html#workers">workers</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a> on Sun Sep 20 2015 11:45:44 GMT+0800 (CST) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>