Skip to content

Commit 037e9ad

Browse files
committed
feat: add NORTHWIND_WS_URL env var
1 parent de8faf0 commit 037e9ad

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import path from 'path';
44
import fs from 'fs';
55

6-
export const expressPort = process.env.PORT || 3000;
7-
export const mongoUri =
6+
export const PORT = process.env.PORT || 3000;
7+
export const MONGODB_URI =
88
process.env.MONGODB_URI || 'mongodb://localhost:27017/graphql-compose-mongoose';
99
export const examplesPath = './examples';
1010

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import http from 'http';
1010
import { SubscriptionServer } from 'subscriptions-transport-ws';
1111
import { execute, subscribe } from 'graphql';
1212
import { mainPage, addToMainPage } from './mainPage';
13-
import { expressPort, getExampleNames, resolveExamplePath } from './config';
13+
import { PORT, getExampleNames, resolveExamplePath } from './config';
1414
import './mongooseConnection';
1515

1616
const app = express();
@@ -34,8 +34,8 @@ app.get('/', (req, res) => {
3434
res.send(mainPage());
3535
});
3636

37-
httpServer.listen(expressPort, () => {
38-
console.log(`🚀🚀🚀 The server is running at http://localhost:${expressPort}/`);
37+
httpServer.listen(PORT, () => {
38+
console.log(`🚀🚀🚀 The server is running at http://localhost:${PORT}/`);
3939

4040
// https://www.apollographql.com/docs/graphql-subscriptions/setup/
4141
SubscriptionServer.create(
@@ -52,7 +52,7 @@ httpServer.listen(expressPort, () => {
5252
},
5353
{
5454
server: httpServer,
55-
path: '/northwind',
55+
path: process.env.NORTHWIND_WS_URL || `ws://localhost:${PORT}/northwind`,
5656
}
5757
);
5858
});

mongooseConnection.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import mongoose from 'mongoose';
4-
import { mongoUri } from './config';
4+
import { MONGODB_URI } from './config';
55

66
mongoose.Promise = Promise;
77

@@ -14,18 +14,18 @@ const opts = {
1414
useFindAndModify: false,
1515
};
1616

17-
mongoose.connect(mongoUri, opts);
17+
mongoose.connect(MONGODB_URI, opts);
1818

1919
export const { connection } = mongoose;
2020
connection.on('error', (e) => {
2121
if (e.message.code === 'ETIMEDOUT') {
2222
console.log(e);
23-
mongoose.connect(mongoUri, opts);
23+
mongoose.connect(MONGODB_URI, opts);
2424
}
2525
console.log(e);
2626
});
2727
connection.on('connected', () => {
28-
console.log(`MongoDB successfully connected to ${mongoUri}`);
28+
console.log(`MongoDB successfully connected to ${MONGODB_URI}`);
2929
});
3030
connection.on('reconnected', () => {
3131
console.log('MongoDB reconnected!');

scripts/seedHelpers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { MongoClient } from 'mongodb';
66
import fs from 'fs';
7-
import { getExampleNames, resolveExamplePath, mongoUri } from '../config';
7+
import { getExampleNames, resolveExamplePath, MONGODB_URI } from '../config';
88

99
function getDBName(uri: string) {
1010
const m = uri.match(/\d{5}\/([a-z0-9_-]{1,})/i);
@@ -17,8 +17,8 @@ let db;
1717

1818
async function mongoConnect() {
1919
if (!db) {
20-
con = await MongoClient.connect(mongoUri, { useNewUrlParser: true });
21-
db = con.db(getDBName(mongoUri));
20+
con = await MongoClient.connect(MONGODB_URI, { useNewUrlParser: true });
21+
db = con.db(getDBName(MONGODB_URI));
2222
}
2323
return db;
2424
}

0 commit comments

Comments
 (0)