Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all 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: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
PORT=8080
DATABASE_CONNECTION_STRING="PUT YOUR DATABASE CONNECTION STRING HERE"
DATABASE_CONNECTION_STRING="<COPY CONNECTION STRING FROM MONGODB ATLAS>"
MONGO_DB_NAME="example_db" # REPLACE THIS WITH A RELEVANT NAME FOR YOUR PROJECT
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,27 @@ Read the `server/` [README](./server/README.md) for more details of the example

We have provided you with an example environment variables file called [`.env.example`](./.env.example). Rename this file to `.env` to use it.

In here you should assign your database connection string to the `DATABASE_CONNECTION_STRING` variable.
In here, you should copy your database connection string (which you can get from the MongoDB Atlas site or the MongoDB extension in VS Code) to the `DATABASE_CONNECTION_STRING` variable.

Make sure your connection string has the correct database name you are trying to connect to and follows this format:
Make sure your connection string follows this format:

```plain
mongodb+srv://<username>:<password>@cluster0.7k5er.mongodb.net/<database_name>
mongodb+srv://<username>:<password>@<cluster id>.mongodb.net
```

For the example app the database name is `example_db`.
If your connection string has a database name appended as below, remove it:

```plain
mongodb+srv://<username>:<password>@<cluster id>.mongodb.net/<database name>
```

For the example app, the database name is `example_db`.

You'll also see the `PORT` for your API in this file. Do not change this `PORT` number.

🛑 **YOUR ENVIRONMENT VARIABLES SHOULD NEVER BE COMMITED AND THE `.env` FILE HAS ALREADY BEEN ADDED TO THE [`.gitignore`](./.gitignore).** 🛑

### Populating The Database
### Populating The Example Database

If you choose to populate your database with some initial data you can do so using seed data. We have provided an example of seed data in the [`data.example/`](./server/data.example) folder in a file called [`profiles.mongodb`](server/data.example/profiles.mongodb).

Expand Down Expand Up @@ -102,3 +108,7 @@ If all's well with the above steps, you should see a list of familiar names. If
## What's Next?!

Now it's time to start building your project.

1. Choose a new database name and update the `MONGO_DB_NAME` value in your `.env` file
2. Create a new `.mongodb` file to populate the database with your data and be sure to update the `use("<database name>")` statement with the name you chose above.
3. Define your new API routes and add to server/index.js
2 changes: 1 addition & 1 deletion client/src/services/profileService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import axios from "axios";

const getAllProfiles = async () => {
const response = await axios.get(`/api/profile`);
const response = await axios.get(`/api/profiles`);

return response.data || [];
};
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.17.1",
"mongoose": "^6.9.2"
"mongodb": "^5.1.0"
},
"devDependencies": {
"concurrently": "^7.0.0",
Expand Down
38 changes: 19 additions & 19 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
require("dotenv").config();

const express = require("express");
const mongoose = require("mongoose");
const MongoClient = require("mongodb").MongoClient;
const bodyParser = require("body-parser");
const cors = require("cors");

// IMPORT YOUR SCHEMAS HERE
require("./models/Profiles"); //This is just an example. Don't forget to delete this

const PORT = process.env.PORT;
const app = express();

// This is where your API is making its initial connection to the database
mongoose.Promise = global.Promise;
mongoose.set("strictQuery", false);
mongoose.connect(process.env.DATABASE_CONNECTION_STRING, {
useNewUrlParser: true,
});

app.use(bodyParser.json());
app.use(cors());

// IMPORT YOUR API ROUTES HERE
// Below is just an example. Don't forget to delete it.
// It's importing and using everything from the profilesRoutes.js file and also passing app as a parameter for profileRoutes to use
require("./routes/profilesRoutes")(app);
// Connect to the database
MongoClient.connect(process.env.DATABASE_CONNECTION_STRING)
.then((client) => {
const db = client.db(process.env.MONGO_DB_NAME);
// IMPORT YOUR API ROUTES HERE
// Below is just an example. Don't forget to delete it.
// It's importing and using everything from the profilesRoutes.js file and also passing app as a parameter for profileRoutes to use
require("./routes/profilesRoutes")(app, db);

const PORT = process.env.PORT;
app.listen(PORT, () => {
console.log(`API running on port ${PORT}`);
});
app.listen(PORT, () => {
console.log(`API running on port ${PORT}`);
});
})
.catch((err) => {
console.error("Error: ", err);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing empty line at the end of this file

10 changes: 0 additions & 10 deletions server/models/Profiles.js

This file was deleted.

106 changes: 77 additions & 29 deletions server/routes/profilesRoutes.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,91 @@
const mongoose = require("mongoose");
const Profile = mongoose.model("profiles");
const { ObjectId } = require("mongodb");

const profileRoutes = (app) => {
app.get(`/api/profile`, async (req, res) => {
const profiles = await Profile.find();
/**
* @param {import('express').Express} app - The Express instance
* @param {import('mongodb').Db} db - The Db instance.
*/
const profilesRoutes = (app, db) => {
/**
* Retrieves the profiles collection from Mongo db
* @returns Collection<Document>
*/
const profilesCollection = () => db.collection("profiles");

return res.status(200).send(profiles);
/**
* Middleware handler for GET requests to /api/profiles path
*/
app.get(`/api/profiles`, async (req, res) => {
try {
// Waits for asynchronous `find()` operation to complete and converts results to array
const profiles = await profilesCollection().find({}).toArray();

return res.status(200).send(profiles);
} catch (e) {
return res
.status(500)
.send(`Error occurred while retrieving profiles: ${e}`);
}
});

app.post(`/api/profile`, async (req, res) => {
const profile = await Profile.create(req.body);
/**
* Middleware handler for POST requests to /api/profiles path
*/
app.post(`/api/profiles`, async (req, res) => {
try {
const profile = await profilesCollection().insertOne(req.body);

return res.status(201).send({
error: false,
profile,
});
return res.status(201).send({
error: false,
profile,
});
} catch (e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be updated to be error instead of e? Will help reinforce that we mean error and not something else such as event

return res
.status(500)
.send(`Error occurred while creating profile: ${e}`);
}
});

app.put(`/api/profile/:id`, async (req, res) => {
const { id } = req.params;

const profile = await Profile.findByIdAndUpdate(id, req.body);
/**
* Middleware handler for PUT requests to /api/profiles/:id path
*/
app.put(`/api/profiles/:id`, async (req, res) => {
try {
// Captures target id from URL
const { id } = req.params;
// Builds query matching `_id` field value matching captured id. `ObjectId()` is needed to convert string value to correct type
const query = { _id: new ObjectId(id) };
const profile = await profilesCollection().replaceOne(query, req.body);

return res.status(202).send({
error: false,
profile,
});
return res.status(202).send({
error: false,
profile,
});
} catch (e) {
return res
.status(500)
.send(`Error occurred while updating profilee: ${e}`);
}
});

app.delete(`/api/profile/:id`, async (req, res) => {
const { id } = req.params;

const profile = await Profile.findByIdAndDelete(id);
/**
* Middleware handler for DELETE requests to /api/profiles/:id path
*/
app.delete(`/api/profiles/:id`, async (req, res) => {
try {
const { id } = req.params;
const query = { _id: new ObjectId(id) };
const profile = await profilesCollection().deleteOne(query);

return res.status(202).send({
error: false,
profile,
});
return res.status(202).send({
error: false,
profile,
});
} catch (e) {
return res
.status(500)
.send(`Error occurred while deleting profiles: ${e}`);
}
});
};

module.exports = profileRoutes;
module.exports = profilesRoutes;