Skip to content

Commit

Permalink
Set up MongoDB Atlas (#15)
Browse files Browse the repository at this point in the history
* Connected to mongo atlas and successfully retried Home resource

* Ran yarn format

* add MONGO_URL to CI test

* fixed formatting and tests

* modify gitignore to ignore all env files in api/config
  • Loading branch information
alicesf2 authored Jan 12, 2021
1 parent 75126c7 commit fb18f4e
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ jobs:
- name: Build application
working-directory: ./api
run: yarn test
env:
MONGO_URL: ${{ secrets.MONGO_URL }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ dist

# TernJS port file
.tern-port

# Env files
api/config/*
1 change: 0 additions & 1 deletion api/config/dev.env

This file was deleted.

1 change: 0 additions & 1 deletion api/config/production.env

This file was deleted.

1 change: 0 additions & 1 deletion api/config/test.env

This file was deleted.

6 changes: 2 additions & 4 deletions api/src/api/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}),
);
Expand Down
22 changes: 11 additions & 11 deletions api/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 4 additions & 0 deletions api/test/home.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand Down

1 comment on commit fb18f4e

@vercel
Copy link

@vercel vercel bot commented on fb18f4e Jan 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

Environment Variable "BACKEND_URL" references Secret "memberdb_tool_backend_url", which does not exist.

Please sign in to comment.