swaf/dist/components/ExpressAppComponent.js

45 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-04-22 15:52:17 +02:00
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import ApplicationComponent from "../ApplicationComponent";
import express from "express";
import Logger from "../Logger";
export default class ExpressAppComponent extends ApplicationComponent {
constructor(port) {
super();
this.port = port;
}
start(app, router) {
return __awaiter(this, void 0, void 0, function* () {
this.server = app.listen(this.port, 'localhost', () => {
Logger.info(`Web server running on localhost:${this.port}.`);
});
router.use(express.json());
router.use(express.urlencoded());
router.use((req, res, next) => {
req.models = {};
req.modelCollections = {};
next();
});
});
}
stop() {
return __awaiter(this, void 0, void 0, function* () {
if (this.server) {
yield this.close('Webserver', this.server, this.server.close);
}
});
}
getServer() {
if (!this.server)
throw 'Server was not initialized.';
return this.server;
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiRXhwcmVzc0FwcENvbXBvbmVudC5qcyIsInNvdXJjZVJvb3QiOiIuLyIsInNvdXJjZXMiOlsiY29tcG9uZW50cy9FeHByZXNzQXBwQ29tcG9uZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7OztBQUFBLE9BQU8sb0JBQW9CLE1BQU0seUJBQXlCLENBQUM7QUFDM0QsT0FBTyxPQUEwQixNQUFNLFNBQVMsQ0FBQztBQUNqRCxPQUFPLE1BQU0sTUFBTSxXQUFXLENBQUM7QUFHL0IsTUFBTSxDQUFDLE9BQU8sT0FBTyxtQkFBb0IsU0FBUSxv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