epic_explorer/server.ts

138 lines
3.6 KiB
TypeScript
Raw Normal View History

2019-07-16 06:17:19 +00:00
import "zone.js/dist/zone-node";
import "reflect-metadata";
const fs = require("fs");
const path = require("path");
const template = fs
.readFileSync(path.join(__dirname, ".", "browser", "index.html"))
.toString();
2019-07-23 19:44:38 +00:00
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM(template);
global["window"] = window;
Object.defineProperty(window.document.body.style, "transform", {
2019-07-16 06:17:19 +00:00
value: () => {
return {
enumerable: true,
configurable: true
};
}
});
2019-07-23 19:44:38 +00:00
global["document"] = window.document;
2019-07-16 06:17:19 +00:00
import { enableProdMode } from "@angular/core";
2019-06-27 06:48:08 +00:00
// Express Engine
2019-07-16 06:17:19 +00:00
import { ngExpressEngine } from "@nguniversal/express-engine";
2019-06-27 06:48:08 +00:00
// Import module map for lazy loading
2019-07-16 06:17:19 +00:00
import { provideModuleMap } from "@nguniversal/module-map-ngfactory-loader";
import express from "express";
import { Request, Response, NextFunction } from "express";
import bodyParser from "body-parser";
//import { logger } from "./server/utils";
import swaggerJSDoc from "swagger-jsdoc";
import { errorMiddleware } from "./server/middlewares";
import {
getRepository,
In,
getConnection,
getConnectionManager
} from "typeorm";
import { resolve } from "path";
import {
BlockchainBlockController,
BlockchainInputController,
BlockchainKernelController,
BlockchainOutputController
} from "./server/controllers";
import { dbConfig } from "./server/ormconfig";
import { config } from "dotenv";
config({ path: resolve(__dirname, "../.env") });
const connectionManager = getConnectionManager();
const connection = connectionManager.create(dbConfig);
import { join } from "path";
2019-07-09 09:22:36 +00:00
2019-06-27 06:48:08 +00:00
// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();
// Express server
const app = express();
2019-07-16 06:17:19 +00:00
const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), "dist");
const controllers = [
new BlockchainBlockController(),
new BlockchainInputController(),
new BlockchainKernelController(),
new BlockchainOutputController()
];
app.use(errorMiddleware);
app.use(bodyParser.json({ limit: "50mb" }));
2019-07-09 09:22:36 +00:00
app.use(bodyParser.urlencoded({ extended: false }));
2019-07-23 19:44:38 +00:00
app.use(
"/api-docs/**",
express.static(path.join(__dirname, "./server/swagger"))
);
2019-07-16 06:17:19 +00:00
app.get("/swagger.json", function(req, res) {
res.setHeader("Content-Type", "application/json");
2019-07-09 09:22:36 +00:00
res.send(
swaggerJSDoc({
2019-07-16 06:17:19 +00:00
swaggerDefinition: require("./swagger/swagger.json"),
apis: ["**/*.ts"]
})
2019-07-09 09:22:36 +00:00
);
});
2019-06-27 06:48:08 +00:00
2019-07-16 06:17:19 +00:00
controllers.forEach(controller => {
app.use("/epic_explorer/v1", controller.router);
});
// Example Express Rest API endpoints
2019-07-23 19:44:38 +00:00
app.get("/epic_explorer/v1/**", (req, res) => {
res.send({ msg: "Api works." });
});
2019-06-27 06:48:08 +00:00
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
2019-07-16 06:17:19 +00:00
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require("./server/main");
2019-06-27 06:48:08 +00:00
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
2019-07-16 06:17:19 +00:00
app.engine(
"html",
ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
providers: [provideModuleMap(LAZY_MODULE_MAP)]
})
);
2019-06-27 06:48:08 +00:00
2019-07-16 06:17:19 +00:00
app.set("views", join(DIST_FOLDER, "browser"));
2019-07-23 19:44:38 +00:00
app.set("view engine", "html");
2019-06-27 06:48:08 +00:00
// Server static files from /browser
2019-07-16 06:17:19 +00:00
app.get(
"*.*",
express.static(join(DIST_FOLDER, "browser"), {
maxAge: "1y"
})
);
2019-06-27 06:48:08 +00:00
// All regular routes use the Universal engine
2019-07-16 06:17:19 +00:00
app.get("*", (req, res) => {
2019-07-23 19:44:38 +00:00
res.sendFile(join(DIST_FOLDER, "browser") + "/index.html", { req });
2019-06-27 06:48:08 +00:00
});
// Start up the Node server
2019-07-16 06:17:19 +00:00
2019-07-09 09:22:36 +00:00
connection
.connect()
.then(() => {
2019-07-16 06:17:19 +00:00
app.listen(PORT, () => {
console.log(`Node Express server listening on http://localhost:${PORT}`);
});
})
.catch(error => {
console.log("connection failed..", error);
});