summaryrefslogtreecommitdiff
path: root/src/app/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/app/index.js')
-rw-r--r--src/app/index.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/app/index.js b/src/app/index.js
new file mode 100644
index 0000000..ee78c16
--- /dev/null
+++ b/src/app/index.js
@@ -0,0 +1,48 @@
+/**
+ * Backend Express HTTP server.
+ * @module app/index
+ */
+
+import express from "express";
+import http from "http";
+import bodyParser from "body-parser";
+import compression from "compression";
+import cors from "cors";
+import morgan from "morgan";
+// import multer from 'multer'
+
+import services from "app/services";
+
+/**
+ * Create the API server.
+ * @param {Knex} knex an existing Knex instance (optional, used in test scripts)
+ * @return {Object} an Express app and HTTP server
+ */
+export default async function createServer(knex) {
+ const app = new express();
+ const server = http.createServer(app);
+
+ app.disable("x-powered-by");
+ app.use(
+ morgan("dev", {
+ skip: (request) => request.method === "OPTIONS",
+ })
+ );
+ app.use(bodyParser.json({ limit: "100mb" }));
+ app.use(bodyParser.urlencoded({ extended: false, limit: "100mb" }));
+ app.use(express.query());
+ app.use(compression());
+ app.set("trust proxy", true);
+
+ if (process.env.NODE_ENV === "development") {
+ const corsMiddleware = cors({
+ origin: ["http://localhost:3000", "http://0.0.0.0:3000"],
+ });
+ app.use(corsMiddleware);
+ app.options("*", corsMiddleware);
+ }
+
+ await services.configure(app, knex);
+
+ return { app, server };
+}