Learning JWT Part 2 - Setup routes, MongoDB === ![](https://i.imgur.com/qFSfCl5.png) --- ###### 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 ![](https://i.imgur.com/QdShMmK.png) - Enter your project name ![](https://i.imgur.com/BS5Zqfu.png) - Add members & permissions(optional), can click create project directly. ![](https://i.imgur.com/SoHYF2B.png) - 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**. ![](https://i.imgur.com/pxrrXBI.png) - Click build a database ![](https://i.imgur.com/KSgvPkY.png) - Choose free plan ![](https://i.imgur.com/giTtGpm.png) - Create a shared cluster, here I am using AWS as cloud provider and HK as region, you can choose whichever you like. ![](https://i.imgur.com/ohjWs3F.png) - Click **Create Cluster** ![](https://i.imgur.com/T4bAZxE.png) - Choose a way of authenticate connection, here I use **username** and **password**, then click **create user** ![](https://i.imgur.com/3QOH8k9.png) - 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. ![](https://i.imgur.com/VGsXPf8.png) --- #### Create collections - Once it redirects to database, click **Browse Collections** ![](https://i.imgur.com/kVu5dTv.png) - Click **Add my own data** ![](https://i.imgur.com/mG0bTCE.png) - Create a database (Collection name should be plural) ![](https://i.imgur.com/3i7KKLQ.png) ![](https://i.imgur.com/5Rewl74.png) --- #### Setting Connection - Click **Connection** ![](https://i.imgur.com/RhZArDD.png) - Choose **Connect to your application** ![](https://i.imgur.com/xl0Tqrl.png) - Choose the **DRIVER** and **VERSION** and copy the connection string below. ![](https://i.imgur.com/mU8svrH.png) #### 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; ```