From 18a2a387b53d7a024600620aae406cee260ae0ba Mon Sep 17 00:00:00 2001 From: Alice Fang Date: Fri, 8 Jan 2021 22:12:05 -0600 Subject: [PATCH 1/5] Connected to mongo atlas and successfully retried Home resource --- .gitignore | 3 +++ api/src/api/home.js | 6 ++---- api/src/app.js | 22 +++++++++++----------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 6704566..74fa474 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,6 @@ dist # TernJS port file .tern-port + +# Env files +api/config/ diff --git a/api/src/api/home.js b/api/src/api/home.js index 9a1cf16..b641d3a 100644 --- a/api/src/api/home.js +++ b/api/src/api/home.js @@ -7,13 +7,11 @@ const Home = require('../models/home'); router.get( '/', errorWrap(async (req, res) => { - // MongoDB connection - // const homeText = await Home.findOne(); - const homeText = "You've connected the database! Isn't it so beautiful???"; + const home = await Home.findOne(); res.status(200).json({ message: `Successfully returned home text`, success: true, - result: homeText, + result: home.text, }); }), ); diff --git a/api/src/app.js b/api/src/app.js index d9901f6..597e748 100644 --- a/api/src/app.js +++ b/api/src/app.js @@ -13,24 +13,24 @@ const bodyParser = require('body-parser'); const app = express(); const { errorHandler } = require('./middleware'); -if (process.env.NODE_ENV != 'production') { +const environment = process.env.NODE_ENV || 'dev'; +if (environment != 'production') { dotenv.config({ - path: path.resolve(__dirname, `../config/${process.env.NODE_ENV}.env`), + path: path.resolve(__dirname, `../config/${environment}.env`), }); } // CONNECTION TO MONGO +mongoose.connect(process.env.MONGO_URL, { + useNewUrlParser: true, + useUnifiedTopology: true, +}); -// mongoose.connect(process.env.MONGO_URL, { -// useNewUrlParser: true, -// useUnifiedTopology: true, -// }); - -// mongoose.Promise = global.Promise; +mongoose.Promise = global.Promise; -// mongoose.connection -// .once('open', () => console.log('Connected to MongoLab instance.')) -// .on('error', error => console.log('Error connecting to MongoLab:', error)); +mongoose.connection + .once('open', () => console.log('Connected to MongoLab instance.')) + .on('error', error => console.log('Error connecting to MongoLab:', error)); app.use(helmet()); app.use(cors()); From da1b724688778d47150769dd27d4951a84071fda Mon Sep 17 00:00:00 2001 From: Alice Fang Date: Sat, 9 Jan 2021 00:55:46 -0600 Subject: [PATCH 2/5] Ran yarn format --- api/src/app.js | 2 +- api/src/appListener.js | 2 +- api/src/middleware/errorWrap.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/api/src/app.js b/api/src/app.js index 597e748..c47f25b 100644 --- a/api/src/app.js +++ b/api/src/app.js @@ -46,7 +46,7 @@ app.get('/', (req, res) => res.json('API working!')); app.get('/favicon.ico', (req, res) => res.status(204)); -app.use(function (req, res, next) { +app.use(function(req, res, next) { next(createError(404)); }); diff --git a/api/src/appListener.js b/api/src/appListener.js index 9c5867d..98a0358 100644 --- a/api/src/appListener.js +++ b/api/src/appListener.js @@ -5,7 +5,7 @@ app.listen(API_PORT, async () => console.log(`API server listening on port ${API_PORT}!`), ); -process.on('unhandledRejection', (error) => { +process.on('unhandledRejection', error => { console.error( 'ERROR: unhandledRejection, did you run `dotenv` before running index?', error.message, diff --git a/api/src/middleware/errorWrap.js b/api/src/middleware/errorWrap.js index c0c2e48..c034113 100644 --- a/api/src/middleware/errorWrap.js +++ b/api/src/middleware/errorWrap.js @@ -2,7 +2,7 @@ * Middleware to allow us to safely handle errors in async/await code * without wrapping each route in try...catch blocks */ -const errorWrap = (fn) => { +const errorWrap = fn => { return (req, res, next) => { return Promise.resolve(fn(req, res, next)).catch(next); }; From 57fe3c8a9e151d8d29aecb163e12651c604a3ad5 Mon Sep 17 00:00:00 2001 From: Alice Fang Date: Tue, 12 Jan 2021 00:22:49 -0600 Subject: [PATCH 3/5] add MONGO_URL to CI test --- .github/workflows/api.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/api.yaml b/.github/workflows/api.yaml index 04d5565..d2858be 100644 --- a/.github/workflows/api.yaml +++ b/.github/workflows/api.yaml @@ -77,3 +77,5 @@ jobs: - name: Build application working-directory: ./api run: yarn test + env: + MONGO_URL: ${{ secrets.MONGO_URL }} From e22ce283b374335562275c0933fede7067c6bb70 Mon Sep 17 00:00:00 2001 From: Alice Fang Date: Tue, 12 Jan 2021 11:24:58 -0600 Subject: [PATCH 4/5] fixed formatting and tests --- api/src/app.js | 4 ++-- api/src/appListener.js | 2 +- api/src/middleware/errorWrap.js | 2 +- api/test/home.test.js | 4 ++++ 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/api/src/app.js b/api/src/app.js index c47f25b..f68d976 100644 --- a/api/src/app.js +++ b/api/src/app.js @@ -30,7 +30,7 @@ mongoose.Promise = global.Promise; mongoose.connection .once('open', () => console.log('Connected to MongoLab instance.')) - .on('error', error => console.log('Error connecting to MongoLab:', error)); + .on('error', (error) => console.log('Error connecting to MongoLab:', error)); app.use(helmet()); app.use(cors()); @@ -46,7 +46,7 @@ app.get('/', (req, res) => res.json('API working!')); app.get('/favicon.ico', (req, res) => res.status(204)); -app.use(function(req, res, next) { +app.use(function (req, res, next) { next(createError(404)); }); diff --git a/api/src/appListener.js b/api/src/appListener.js index 98a0358..9c5867d 100644 --- a/api/src/appListener.js +++ b/api/src/appListener.js @@ -5,7 +5,7 @@ app.listen(API_PORT, async () => console.log(`API server listening on port ${API_PORT}!`), ); -process.on('unhandledRejection', error => { +process.on('unhandledRejection', (error) => { console.error( 'ERROR: unhandledRejection, did you run `dotenv` before running index?', error.message, diff --git a/api/src/middleware/errorWrap.js b/api/src/middleware/errorWrap.js index c034113..c0c2e48 100644 --- a/api/src/middleware/errorWrap.js +++ b/api/src/middleware/errorWrap.js @@ -2,7 +2,7 @@ * Middleware to allow us to safely handle errors in async/await code * without wrapping each route in try...catch blocks */ -const errorWrap = fn => { +const errorWrap = (fn) => { return (req, res, next) => { return Promise.resolve(fn(req, res, next)).catch(next); }; diff --git a/api/test/home.test.js b/api/test/home.test.js index f7e3d6f..a3f1807 100644 --- a/api/test/home.test.js +++ b/api/test/home.test.js @@ -2,6 +2,10 @@ const app = require('../src/app'); const mongoose = require('mongoose'); const request = require('supertest'); +afterAll(async () => { + await mongoose.connection.close(); +}); + describe('GET / ', () => { test('API should return working message', async () => { const response = await request(app).get('/'); From 5fdbb14beadb585bdeff33c9142a109b99f73891 Mon Sep 17 00:00:00 2001 From: Alice Fang Date: Tue, 12 Jan 2021 11:34:22 -0600 Subject: [PATCH 5/5] modify gitignore to ignore all env files in api/config --- .gitignore | 2 +- api/config/dev.env | 1 - api/config/production.env | 1 - api/config/test.env | 1 - 4 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 api/config/dev.env delete mode 100644 api/config/production.env delete mode 100644 api/config/test.env diff --git a/.gitignore b/.gitignore index 74fa474..9eb0d33 100644 --- a/.gitignore +++ b/.gitignore @@ -104,4 +104,4 @@ dist .tern-port # Env files -api/config/ +api/config/* \ No newline at end of file diff --git a/api/config/dev.env b/api/config/dev.env deleted file mode 100644 index dba90cf..0000000 --- a/api/config/dev.env +++ /dev/null @@ -1 +0,0 @@ -MONGO_URL=dev-mongo \ No newline at end of file diff --git a/api/config/production.env b/api/config/production.env deleted file mode 100644 index bd0d1d5..0000000 --- a/api/config/production.env +++ /dev/null @@ -1 +0,0 @@ -MONGO_URL=production-mongo \ No newline at end of file diff --git a/api/config/test.env b/api/config/test.env deleted file mode 100644 index 5b0bcb6..0000000 --- a/api/config/test.env +++ /dev/null @@ -1 +0,0 @@ -MONGO_URL=test-mongo \ No newline at end of file