Skip to content

Commit

Permalink
Merge pull request #7 from bridge-school/cohort-8/update
Browse files Browse the repository at this point in the history
[UPDATE] Cohort 8
  • Loading branch information
Purvi Kanal authored Jul 30, 2019
2 parents 686b0e6 + fc22b00 commit 05678ba
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 24 deletions.
56 changes: 40 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,66 @@
## Installation

```sh
$ yarn install
$ npm install
```

## Run the app locally
## Setup the app for local development

We are using Firebase [cloud fire store](https://firebase.google.com/docs/firestore/quickstart) for our database.

To setup a connection to the database:

1. Have a look in your project slack channel for a pinned JSON file called `firebase-credentials.json`.
2. Create a `firebase-credentials.json` file in the root directory of this repository and copy the contents from the file in the slack channel
3. Run `yarn start:local` and if everything is running smoothly you should see no errors
4. To test your server is running correctly, go to `http://localhost:8081/health` in your browser. If you see `{ ok: 'OK' }` then everything is running as expected. If you're not seeing this, reach out to your tech lead or mentor over slack to help you debug!

To query the database you will need to `require` the `db` instance that is exported from `db/index.js`. You can use the [firebase docs](https://firebase.google.com/docs/firestore/query-data/get-data) and have a look under the node.js tab for examples.

## Run the app for local development

```sh
$ yarn start:local
$ npm run start:local
```

## Run the app in production

```
$ yarn start
$ npm start
```

## Middleware
## Folder Structure

Middleware functions are functions that have access to the request object (`req`), the response object (`res`), and the next middleware function in the application’s request-response cycle.
```
|-api
|-db
|-middleware
|-routes
|---health
|-utils
```

Middleware can be at the application level or at the router level.
### `api`

## Database
This is where you add new routes, see the example `health` route.

We are using Firebase [cloud fire store](https://firebase.google.com/docs/firestore/quickstart) for our database.
### `db`

To setup a connection to the database:
This is where the Firebase connection is configured. To query the database you will need to `require` the `db` instance that is exported from `db/index.js`.

1. Have a look in your project slack channel for a pinned JSON file called `firebase-credentials.json`.
2. Create a `firebase-credentals.json` file in the root directory of this repository and copy the contents from the file in the slack channel
3. Run `yarn start:local` and if everything is running smoothly you should see no errors
### `middleware`

To query the database you will need to `require` the `db` instance that is exported from `db/index.js`. You can use the [firebase docs](https://firebase.google.com/docs/firestore/query-data/get-data) and have a look under the node.js tab for examples.
Middleware functions are functions that have access to the request object (`req`), the response object (`res`), and the next middleware function in the application’s request-response cycle.

Middleware can be at the application level or at the router level. You won't be interacting with this folder much.

### `routes`

This is where all the logic for your endpoints will live. You should make a new folder under `routes` for each set of endpoints you will write, similar to the `health` folder that exists as an example.

## Testing
### `utils`

Using `jest` and `supertest` for API testing.
This is a multi-purpose folder for any extra utility functions that you might want to reuse throughout your app.

## Commit messages

Expand Down
12 changes: 10 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"supertest": "^3.1.0"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.16.3",
"firebase-admin": "^6.0.0",
"morgan": "^1.9.0",
Expand Down
2 changes: 1 addition & 1 deletion src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require("express");

const { healthRouter } = require('../routes/health/health.router')
const { healthRouter } = require("../routes/health/health.router");

const router = express.Router();
router.use("/health", healthRouter);
Expand Down
14 changes: 10 additions & 4 deletions src/db/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const admin = require("firebase-admin");
const serviceAccount = require("../../firebase-credentials.json");

// initialize firebase store
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
try {
const serviceAccount = require("../../firebase-credentials.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
} catch (e) {
throw new Error(
"Please add the firebase-credentials.json file to your root folder found in your project's Slack channel"
);
}

const db = admin.firestore();

Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require("express");
const morgan = require("morgan");
// const admin = require("firebase-admin");
const cors = require("cors");

const router = require("./api");
const { logger } = require("./utils/logger");
Expand All @@ -14,7 +14,14 @@ const port = process.env.PORT || 8081;

logger.info("🤖 Initializing middleware");

// This piece of middleware creates the logs that you see when
// you hit an endpoint in your terminal. It's here to help you debug.
app.use(morgan("tiny", { stream: logger.stream }));
app.use(
cors({
origin: `http://${process.env.PROJECT_NAME}-frontend.bridgeschoolapp.io`
})
);
app.use("/", router);
app.use(errorHandler);

Expand Down

0 comments on commit 05678ba

Please sign in to comment.