Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up MongoDB Atlas #15

Merged
merged 5 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/
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
24 changes: 12 additions & 12 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 All @@ -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));
});

Expand Down
2 changes: 1 addition & 1 deletion api/src/appListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion api/src/middleware/errorWrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down