From 1fbf5ffbabc1ea2924e68c735e4d2b87643b5fc0 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 26 Jun 2020 14:44:00 +0300 Subject: build: conntect to database correctly --- index.ts | 15 ++++++++++++--- populateDb.ts | 10 +++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/index.ts b/index.ts index 8b01536..9ba0431 100644 --- a/index.ts +++ b/index.ts @@ -4,7 +4,17 @@ import app from './app'; mongoose.Promise = Promise; -mongoose.connect('mongodb://localhost:27017/which', { useNewUrlParser: true }); + +const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/which'; +const PORT = process.env.PORT || 3030; + +mongoose.connect(MONGODB_URL, { + useNewUrlParser: true, + useUnifiedTopology: true, + useCreateIndex: true, + useFindAndModify: false, + family: 4 // Use IPv4, skip trying IPv6 +}); const db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); @@ -18,6 +28,5 @@ app.on('connection', connection => app.channel('everybody').join(connection)); app.publish(() => app.channel('everybody')); -const port = 3030; -app.listen(port).on('listening', () => console.log(`Feathers server listening on localhost:${port}`)); +app.listen(PORT).on('listening', () => console.log(`Feathers server listening on localhost:${PORT}`)); diff --git a/populateDb.ts b/populateDb.ts index e978403..b3e46af 100644 --- a/populateDb.ts +++ b/populateDb.ts @@ -5,7 +5,15 @@ import { User, Poll, Vote } from 'which-types'; import app from './app'; -mongoose.connect('mongodb://localhost:27017/which', { useNewUrlParser: true }); +const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/which'; + +mongoose.connect(MONGODB_URL, { + useNewUrlParser: true, + useUnifiedTopology: true, + useCreateIndex: true, + useFindAndModify: false, + family: 4 // Use IPv4, skip trying IPv6 +}); const POLLS_AMOUNT = 20; -- cgit v1.2.3