twokeys-server
Version:
Server for 2Keys
62 lines (58 loc) • 2.25 kB
JavaScript
;
/**
Copyright 2018 Kishan Sambhi
This file is part of 2Keys.
2Keys is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2Keys is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with 2Keys. If not, see <https://www.gnu.org/licenses/>.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @overview Server starter for 2Keys
*/
const express_1 = __importDefault(require("express"));
const body_parser_1 = __importDefault(require("body-parser"));
const fs_1 = require("fs");
const api_1 = __importDefault(require("./routes/api"));
const logger_1 = __importDefault(require("./util/logger"));
const constants_1 = require("./util/constants");
const app = express_1.default();
exports.app = app;
const logger = new logger_1.default({
name: "server",
});
app.use(body_parser_1.default.json());
app.use("/api", api_1.default);
const server = (port = constants_1.DEFAULT_PORT, argv) => {
app.listen(port, () => {
logger.info("Server now listenning on port " + port);
logger.debug("PID: " + process.pid);
});
if (argv.hasOwnProperty("pid-file") && argv["pid-file"]) {
logger.debug(`Writing pid file to ${argv["pid-file"]}...`);
fs_1.writeFile(argv["pid-file"], process.pid, (err) => {
if (err) {
logger.throw(err);
}
logger.info("PID file Written.");
});
}
};
// Error handler
app.use((err, req, res, next) => {
logger.err(`An error was encountered on router ${req.originalUrl}`);
logger.throw_noexit(err);
next(err);
});
exports.default = server;
//# sourceMappingURL=index.js.map