diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2021-10-17 02:52:05 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2021-10-17 02:52:05 +0200 |
| commit | 06ecdf2af182034496e2123852deee4a58de1043 (patch) | |
| tree | c8d4eb9664dd368bee5a4bf73dd1e02015ecaf39 /src/app/index.js | |
making a shoebox
Diffstat (limited to 'src/app/index.js')
| -rw-r--r-- | src/app/index.js | 48 |
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 }; +} |
