From 0d870be23c7129645a7f4b16a812d008671d1a0d Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Tue, 24 Nov 2020 19:28:57 +0100 Subject: [PATCH] sample implementation of db connection ref #1 #2 --- .env.example | 7 +++++++ src/app.ts | 23 ++++++++++++++++------- tsconfig.json | 5 ++--- 3 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..f9c3dc1 --- /dev/null +++ b/.env.example @@ -0,0 +1,7 @@ +APP_PORT=4010 +DB_TYPE=bla +DB_HOST=bla +DB_PORT=bla +DB_USER=bla +DB_PASSWORD=bla +DB_NAME=bla \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index 57a6519..1df076b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,9 +1,18 @@ import * as dotenvSafe from 'dotenv-safe'; -dotenvSafe.config(); import express from 'express'; -const app = express(); -const PORT = 4010; -app.get('/', (req, res) => res.send('Express + TypeScript Server')); -app.listen(PORT, () => { - console.log(`⚡️[server]: Server is running at http://localhost:${PORT}`); -}); +import { createConnection } from 'typeorm'; + +dotenvSafe.config(); + +createConnection() + .then((connection) => { + const app = express(); + const PORT = process.env.APP_PORT || 4010; + app.get('/', (req, res) => res.send('Express + TypeScript Server')); + app.listen(PORT, () => { + console.log(`⚡️[server]: Server is running at http://localhost:${PORT}`); + }); + }) + .catch((err) => { + console.log(err); + }); diff --git a/tsconfig.json b/tsconfig.json index f29e259..b569d8b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,10 @@ { "compilerOptions": { "target": "es2017", - "module": "esnext", + "module": "commonjs", "rootDir": "./src", "outDir": "./build", "esModuleInterop": true, - "strict": true, - "moduleResolution": "node" + "strict": true } } \ No newline at end of file