Learning JWT Part 2 - Setup routes, MongoDB
===

---
###### tags: `JWT`
Credit to Net Ninja's YT tutorial: [Node Auth with JWT](https://www.youtube.com/playlist?list=PL4cUxeGkcC9iqqESP8335DA5cRFp8loyp)
## Project Setup
1. Create a project folder and open terminal.
2. Run `npm inint -y`.
3. Install the following packages:
- `express`
- `dotenv`
- `jsonwebtoken`
- `nodemon` (Can install locally or globally)
- Globally: `npm i nodemon -g`
- Locally: `npm i nodemon`
- `mongoose`
- `ejs`
- `validator`
### MongoDB setup
Go to [Mongodb](https://www.mongodb.com/) and sign up:
- Create a new project

- Enter your project name

- Add members & permissions(optional), can click create project directly.

- If it showed a warning: Current IP Address not added. You will not be able to connect to databases from this address. Click **Add current IP Address**.

- Click build a database

- Choose free plan

- Create a shared cluster, here I am using AWS as cloud provider and HK as region, you can choose whichever you like.

- Click **Create Cluster**

- Choose a way of authenticate connection, here I use **username** and **password**, then click **create user**

- Add additional IP address if you want to share to other people, otherwise, you can leave as it is (Make sure your IP address has added already).Once it's done, click **Finish and Close**. It should redirect to database.

---
#### Create collections
- Once it redirects to database, click **Browse Collections**

- Click **Add my own data**

- Create a database (Collection name should be plural)


---
#### Setting Connection
- Click **Connection**

- Choose **Connect to your application**

- Choose the **DRIVER** and **VERSION** and copy the connection string below.

#### Connect to MongoDB
Now that we have a unique connection string
---
### Server
In this project, we will have 5 routes.
| Route | Request | Goal |
| -------- | -------- | ------------------------------|
| /signup | GET | Sign up page |
| /login | GET | Login page |
| /signup | POST | Create a new user in database |
| /login | POST | Authenticate current user |
| /logout | GET | Logout user |
#### MVC (Model, View, Controller)
> Reference: [MVC](https://developer.mozilla.org/en-US/docs/Glossary/MVC)
Folder structure:
This project was using **ejs** to build interface, will switch to **React** afterwards.
```
Project Name
|-controllers
| |-authControllers.js
|-models
| |-user.js
|-routes
| |-authRoutes.js
|-utility
| |-handleError.js
|-views
| |- components
| | |-footer.ejs
| | |-header.ejs
| |-home.ejs
| |-login.ejs
| |-signup.ejs
|-.env
|-app.js
```
---
### Rutes
Inside **routes** floder, create a **authRoutes.js** file, it will manage all routes here.
Here we can use a method `Router()` from express to create a new router object and add middleware and HTTP method routes.
Before we use `Router()`, we use `app.get(...)` or `app.post(...)`like the code below.
```javascript=
const app = express();
app.get("/signup", (req, res) => {...})
app.post("/signup", (req, res) => {...})
app.get("/login", (req, res) => {...});
app.post("/login", (req, res) => {...});
```
Now we have moved our routers out of `app.js`, we can use `Router()` like code below.
> Rferences:
> [express-router](https://expressjs.com/en/5x/api.html#router)
```javascript=
const { Router } = require("express");
const router = Router();
// Create routes here
router.get("/signup", (req, res) => {...});
router.post("/signup", (req, res) => {...});
router.get("/login", (req, res) => {...});
router.post("/login", (req, res) => {...});
module.exports = router;
```