import express from "express"; import cors from "cors"; import path from "path"; import morgan from "morgan"; import socketio from "socket.io"; import ascii from "./ascii"; import db from "./db"; const pg = require("pg"); delete pg.native; import { connectUpstream } from "./joystream/ws"; import { setupSocket } from "./socket"; import { importEvents } from "./joystream"; import { allowedOrigins, port } from "./config.json"; const PORT: number = process.env.PORT ? +process.env.PORT : +port; const app = express(); const server = app.listen(PORT, async () => { // handle arguments const args = process.argv.slice(2); if (args.length) return importEvents(args); console.log(`[Express] Listening on port ${PORT}`, ascii); const api = await connectUpstream(); const io: any = socketio(server); setupSocket(io, api); }); app.use(morgan("dev")); app.use(cors({ credentials: true, origin: allowedOrigins.split(",") })); // body parsing middleware app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use(require("body-parser").text()); app.use("/api", require("./api")); app.use( "/static", express.static(path.resolve(__dirname, "..", "build", "static")) ); app.get("/manifest.json", (req: any, res: any) => { res.sendFile(path.resolve(__dirname, "..", "build", "manifest.json")); }); app.get("/favicon.png", (req: any, res: any) => { res.sendFile(path.resolve(__dirname, "..", "build", "favicon.png")); }); app.use("*", express.static(path.resolve(__dirname, "..", "build"))); // error handling endware app.use((err: any, req: any, res: any, next: any) => { console.error(err); console.error(err.stack); res.status(err.status || 500).send(err.message || "Internal server error."); next(); });