UNPKG

2.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var express = require("express");
4var cors = require("cors");
5var cookieParser = require("cookie-parser");
6var bodyParser = require("body-parser");
7var libs_1 = require("../libs");
8var listEndpoints = require('express-list-endpoints');
9exports.Router = express.Router;
10var staticRoutes_1 = require("./middleware/staticRoutes");
11exports.staticRoutes = staticRoutes_1.staticRoutes;
12var IS_PRODUCTION = process.env.NODE_ENV === 'production';
13function formatErrorStack(stack) {
14 if (stack === void 0) { stack = ''; }
15 var lines = stack.split('\n');
16 var message = lines[0];
17 lines.shift();
18 return {
19 message: message,
20 stack: lines.map(function (line) { return line.trim(); }),
21 };
22}
23function sendError(code, res, err) {
24 var error = IS_PRODUCTION ? err.message : formatErrorStack(err.stack);
25 res.status(code).send({
26 status: code,
27 error: error,
28 });
29}
30exports.sendError = sendError;
31function app(options) {
32 if (options === void 0) { options = {}; }
33 var app = express()
34 .use(cors(options.cors))
35 .use(bodyParser.json(options.json))
36 .use(cookieParser());
37 if (options.static) {
38 app.use(express.static(options.static));
39 }
40 return app;
41}
42exports.app = app;
43function router() {
44 var routes = express.Router();
45 return routes;
46}
47exports.router = router;
48function routes(router) {
49 var items = listEndpoints(router);
50 return items.reduce(function (acc, next) {
51 var index = acc.findIndex(function (r) { return r.path === next.path; });
52 if (index > -1) {
53 var route = items[index];
54 route.methods = libs_1.R.uniq(route.methods.concat(next.methods));
55 return acc;
56 }
57 return acc.concat([next]);
58 }, []);
59}
60exports.routes = routes;
61function logStarted(port, args) {
62 if (args === void 0) { args = {}; }
63 var PACKAGE = require(libs_1.fsPath.resolve('./package.json'));
64 libs_1.log.info("\n> Ready on " + libs_1.log.cyan('localhost') + ":" + libs_1.log.magenta(port));
65 libs_1.log.info();
66 libs_1.log.info.gray(" name: " + libs_1.log.white(PACKAGE.name) + "@" + PACKAGE.version);
67 libs_1.log.info.gray(" dev: " + libs_1.constants.IS_DEV);
68 libs_1.log.info();
69}
70exports.logStarted = logStarted;
71//# sourceMappingURL=express.js.map
\No newline at end of file