UNPKG

irrelon-reactor-autonet

Version:

A module for automatically creating groups of self-networking services.

175 lines (141 loc) 5.58 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _express = require('express'); var _express2 = _interopRequireDefault(_express); var _bodyParser = require('body-parser'); var _bodyParser2 = _interopRequireDefault(_bodyParser); var _freeport = require('freeport'); var _freeport2 = _interopRequireDefault(_freeport); var _EventEmitter2 = require('./EventEmitter'); var _EventEmitter3 = _interopRequireDefault(_EventEmitter2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var Server = function (_EventEmitter) { _inherits(Server, _EventEmitter); function Server(name) { _classCallCheck(this, Server); var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Server).call(this, name)); _this._routes = {}; _this.app = (0, _express2.default)(); // Enable JSON support _this.app.use(_bodyParser2.default.json()); // Enable url-encoding support _this.app.use(_bodyParser2.default.urlencoded({ extended: true })); // Enable pre-flight CORS _this.app.use(function (req, res, next) { res.header('Access-Control-Allow-Origin', req.headers.origin); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, x-irrelon-auth'); // Intercept OPTIONS method if (req.method === 'OPTIONS') { res.sendStatus(200); } else { next(); } }); return _this; } _createClass(Server, [{ key: 'start', value: function start(host, port, callback) { var self = this; if (!port || port === 0 || port === '0' || port === undefined) { // Get a free port first (0, _freeport2.default)(function (err, availablePort) { if (err) { throw err; } self._start(availablePort, host, function (err, data) { if (!err) { //self.log('Listening at ' + host + ' on port ' + availablePort); callback(false, self.server.address()); } }); }); } else { self._start(port, host, function () { self.log('Listening at ' + host + ' on port ' + port); callback(false); }); } } }, { key: '_start', value: function _start(port, host, cb) { this.server = this.app.listen(port, host, cb); } }, { key: 'staticRoute', value: function staticRoute(path, filePath) { this.log('Service static files at ' + path + ' from ' + __dirname + filePath); this.app.use(path, _express2.default.static(__dirname + filePath)); } }, { key: 'optionsRoute', value: function optionsRoute(path, callback) { this._routes[path] = this._routes[path] || { methods: [], protocol: 'http://' }; this._routes[path].methods.push('OPTIONS'); this.app.options(path, callback); } }, { key: 'headRoute', value: function headRoute(path, callback) { this._routes[path] = this._routes[path] || { methods: [], protocol: 'http://' }; this._routes[path].methods.push('HEAD'); this.app.head(path, callback); } }, { key: 'getRoute', value: function getRoute(path, callback) { this._routes[path] = this._routes[path] || { methods: [], protocol: 'http://' }; this._routes[path].methods.push('GET'); this.app.get(path, callback); } }, { key: 'putRoute', value: function putRoute(path, callback) { this._routes[path] = this._routes[path] || { methods: [], protocol: 'http://' }; this._routes[path].methods.push('PUT'); this.app.put(path, callback); } }, { key: 'postRoute', value: function postRoute(path, callback) { this._routes[path] = this._routes[path] || { methods: [], protocol: 'http://' }; this._routes[path].methods.push('POST'); this.app.post(path, callback); } }, { key: 'deleteRoute', value: function deleteRoute(path, callback) { this._routes[path] = this._routes[path] || { methods: [], protocol: 'http://' }; this._routes[path].methods.push('DELETE'); this.app.delete(path, callback); } }]); return Server; }(_EventEmitter3.default); exports.default = Server;