UNPKG

pao-aop-server

Version:

基于pao-aop的服务端框架

142 lines (140 loc) 6.58 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "express", "http", "https", "path", "fs", "pao-aop", "../base", "connect-history-api-fallback"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var express = require("express"); var http = require("http"); var https = require("https"); var path = require("path"); var fs = require("fs"); var pao_aop_1 = require("pao-aop"); var base_1 = require("../base"); var connecthistoryapifallback = require("connect-history-api-fallback"); /** 编码 */ var ENCODING = 'utf8'; /** * HTTPS 应用服务配置 */ var HttpsOption = /** @class */ (function () { function HttpsOption() { } return HttpsOption; }()); exports.HttpsOption = HttpsOption; /** * 插件:Express服务 * 提供Express服务 */ var ExpressServer = /** @class */ (function (_super) { __extends(ExpressServer, _super); /** * Express服务 * @param hostName 服务主机 * @param httpPort http应用端口 * @param httpsPort https应用端口 * @param httpsOption https配置 */ function ExpressServer(hostName, httpPort, httpsPort, httpsOption) { var _this = _super.call(this) || this; _this.hostName = hostName; _this.httpPort = httpPort; _this.httpsPort = httpsPort; _this.httpsOption = httpsOption; _this.hostName = hostName || 'localhost'; return _this; } ExpressServer.prototype.onStart = function () { var _this = this; var app = express(); exports.expressApp = app; pao_aop_1.log('express', '设定静态目录'); // 设定找不到的get请求跳转index.html app.use(connecthistoryapifallback()); // 静态目录设定 if (this.staticDirectory) { var staticPath = path.join(process.cwd(), this.staticDirectory); pao_aop_1.log('express', "express\u5E94\u7528\u542F\u52A8\u76EE\u5F55[" + process.cwd() + "]"); pao_aop_1.log('express', "\u9759\u6001\u76EE\u5F55'[" + staticPath + "]"); app.use(express.static(staticPath)); } pao_aop_1.log('express', '过滤服务目录'); // 过滤服务文件夹,不能通过get请求访问 app.use(function (req, res, next) { // 设置指定文件目录 var server = /(\/server\/)/g; if (server.test(req.path)) { // 设置utf - 8编码格式 res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' }); res.end('请求非法目录文件'); } else { next(); } }); // 设置端口 app.set('port', this.httpPort); app.set('host', this.hostName); app.set('trust proxy', true); // 启动服务 this.httpServer = http.createServer(app); this.httpServer.listen(this.httpPort, this.hostName, function () { pao_aop_1.log('express', "Express HTTP \u670D\u52A1\u5F00\u59CB\u5728\u7AEF\u53E3" + _this.hostName + ":" + _this.httpPort + "\u76D1\u542C"); }); if (this.httpsPort && this.httpsOption) { try { //读取公私钥文件 var privateKey = fs.readFileSync(path.join(process.cwd(), this.httpsOption.privateKeyPath), ENCODING), certificate = fs.readFileSync(path.join(process.cwd(), this.httpsOption.certificatePath), ENCODING); this.httpsServer = https.createServer({ key: privateKey, cert: certificate }, app); this.httpsServer.listen(this.httpsPort, this.hostName, function () { pao_aop_1.log('express', "Express HTTPS \u670D\u52A1\u5F00\u59CB\u5728\u7AEF\u53E3" + _this.hostName + ":" + _this.httpsPort + "\u76D1\u542C"); }); } catch (error) { pao_aop_1.log('express', "Express HTTPS \u670D\u52A1\u5F00\u59CB\u5F02\u5E38\uFF0C" + error.message); } } }; ExpressServer.prototype.onStop = function () { this.httpServer.close(); if (this.httpsServer) { this.httpsServer.close(); } }; ExpressServer = __decorate([ pao_aop_1.addon('ExpressServer', 'Express服务', '提供Express服务'), __metadata("design:paramtypes", [String, Number, Number, HttpsOption]) ], ExpressServer); return ExpressServer; }(base_1.BaseServer)); exports.ExpressServer = ExpressServer; }); //# sourceMappingURL=index.js.map