加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.js 2.93 KB
一键复制 编辑 原始数据 按行查看 历史
yulifu 提交于 2017-02-16 12:29 . add comment of https server
'use strict';
var http = require('http');
var Y = require('./Y');
var WebApp = require('./web/Application');
var InvalidConfigException = require('./core/InvalidConfigException');
var Hook = require('./core/Hook');
var Logger = require('./log/Logger');
var Cookie = require('./web/Cookie');
var WebRestful = require('./web/Restful');
var WebRequest = require('./web/Request');
var WebResponse = require('./web/Response');
var Session = require('./session/Session');
var WebController = require('./web/Controller');
/**
* 入口
*/
class YNode {
/**
* constructor
*
* @param JSON config 配置信息
*/
constructor(config) {
if(undefined === config) {
throw new InvalidConfigException('The app config is required');
}
this.config = config;
this.server = null;
this.app = new WebApp(config);
}
// debug
debug(res, error) {
res.setHeader('Content-Type', 'text/plain');
res.writeHead(500);
res.end(true === this.config.debug ? error.message + '\n' + error.stack :
'The server encountered an internal error');
}
// web
requestListenerWeb(req, res) {
try {
this.app.requestListener(req, res);
} catch(e) {
this.debug(res, e);
}
}
// restful
requestListenerRestful(req, res) {
try {
WebRestful.requestListener(req, res);
} catch(e) {
this.debug(res, e);
}
}
// handler
handler(req, res) {
Hook.getInstance().trigger(req, res, () => {
if(true === this.config.useRestful) {
this.requestListenerRestful(req, res);
return;
}
this.requestListenerWeb(req, res);
});
}
/**
* 获取 http server
*
* @return http server
*/
getServer() {
return http.createServer(this.handler.bind(this));
}
/**
* listen
*
* @param int port
* @param Function callback
*
* If you want to create HTTPS server you can do so as shown here
*
* var https = require('https');
* var YNode = require('ynode');
* var app = new YNode({ ... });
* https.createServer({ ... }, app.handler.bind(app)).listen(443);
*
*/
listen(port, callback) {
this.server = this.getServer();
this.server.listen(port, callback);
}
}
/**
* handler
*/
YNode.Y = Y;
/**
* Hook
*/
YNode.Hook = Hook;
/**
* Logger
*/
YNode.Logger = Logger;
/**
* Cookie
*/
YNode.Cookie = Cookie;
/**
* Web restful
*/
YNode.WebRestful = WebRestful;
/**
* Web request
*/
YNode.WebRequest = WebRequest;
/**
* Web response
*/
YNode.WebResponse = WebResponse;
/**
* Session
*/
YNode.Session = Session;
/**
* WebController handler
*/
YNode.WebController = WebController;
module.exports = YNode;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化