summaryrefslogtreecommitdiff
path: root/src/app/index.js
blob: 3c12cd576812a112d7ff9478f7c38b3479221f63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * 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"],
      origin: true,
    });
    app.use(corsMiddleware);
    app.options("*", corsMiddleware);
  }

  await services.configure(app, knex);

  return { app, server };
}