From be91d529021afe4bcda31aa17659a721edb0f7f1 Mon Sep 17 00:00:00 2001 From: Roland Bernard <rolbernard@unibz.it> Date: Sat, 17 Apr 2021 12:56:32 +0200 Subject: [PATCH] Added the possibility to add custom headers --- server/src/config.ts | 8 ++++++-- server/src/headers.ts | 13 +++++++++++++ server/src/index.ts | 8 +++++--- server/src/setupTests.ts | 2 +- server/src/v1/user.ts | 1 - server/tsconfig.json | 9 +++++++++ 6 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 server/src/headers.ts diff --git a/server/src/config.ts b/server/src/config.ts index f083c08..0c932c1 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -1,9 +1,13 @@ -export const port = "development"; +export const port = 8000; export const keys = { private: '/etc/ssl/localcerts/cert.key', - public: '/etc/ssl/localcerts/cert.pem' + public: '/etc/ssl/localcerts/cert.pem', +}; + +export const headers = { + 'Access-Control-Allow-Origin': '*', }; export const environment = "development"; diff --git a/server/src/headers.ts b/server/src/headers.ts new file mode 100644 index 0000000..b1ff6b9 --- /dev/null +++ b/server/src/headers.ts @@ -0,0 +1,13 @@ + +import { NextFunction, Request, Response } from 'express'; + +import { headers } from './config'; + +export function addDefaultHeaders(_req: Request, res: Response, next: NextFunction) { + let header : keyof typeof headers; + for (header in headers) { + res.header(header, headers[header]); + } + next(); +} + diff --git a/server/src/index.ts b/server/src/index.ts index 4c8a0dd..9b13dd2 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -2,11 +2,13 @@ import express, { Request, Response, NextFunction } from 'express'; import { json as bodyJson } from 'body-parser'; +import { port } from './config'; +import { addDefaultHeaders } from './headers'; import v1 from './v1'; const app = express(); -const PORT = 8000; +app.use(addDefaultHeaders); app.use(bodyJson()); app.use('/v1', v1); @@ -25,7 +27,7 @@ app.use((_err: Error, _req: Request, res: Response, _next: NextFunction) => { }); }); -app.listen(PORT, () => { - console.log(`[server] Server is running at https://localhost:${PORT}`); +app.listen(port, () => { + console.log(`[server] Server is running at https://localhost:${port}`); }); diff --git a/server/src/setupTests.ts b/server/src/setupTests.ts index 0662879..7ce45c2 100644 --- a/server/src/setupTests.ts +++ b/server/src/setupTests.ts @@ -1,3 +1,3 @@ -// Setup the testing environment right here +import '@testing-library/jest-dom'; diff --git a/server/src/v1/user.ts b/server/src/v1/user.ts index 51373ad..f23b5b8 100644 --- a/server/src/v1/user.ts +++ b/server/src/v1/user.ts @@ -2,7 +2,6 @@ import express from 'express'; import database from '../database'; -import { isOfType } from '../util'; import { requireVerification } from './auth'; const user = express(); diff --git a/server/tsconfig.json b/server/tsconfig.json index 5802e66..340c616 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -3,6 +3,15 @@ "target": "es6", "lib": [ ], + "allowJs": true, + "skipLibCheck": true, + "allowSyntheticDefaultImports": true, + "forceConsistentCasingInFileNames": true, + "noFallthroughCasesInSwitch": true, + "moduleResolution": "node", + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, "module": "commonjs", "rootDir": "src", "outDir": "build", -- GitLab