Skip to content

Instantly share code, notes, and snippets.

@thuyanduong
Last active February 6, 2023 20:55
Show Gist options
  • Save thuyanduong/34e45f54b220a31aca38c3c32e79a777 to your computer and use it in GitHub Desktop.
Save thuyanduong/34e45f54b220a31aca38c3c32e79a777 to your computer and use it in GitHub Desktop.
Routers, Controllers, Models Workshop with Sankofa

Express Architecture Review (10 minutes)

Set up (10 minutes)

  1. Use your existing backend code with your migrations/seeds
  2. touch index.js to create your main server file
  3. npm install express
  4. npm start script
  5. Set up your server file and db file
//index.js
const express = require("express")
const app = express()
const PORT = process.env.PORT || 8000 

app.get("/robots", (req, res) => {
  res.send("here are the robots")
})

app.listen(PORT, function(){
  console.log("Server started on port: ", PORT);
})
//db.js
const { Pool } = require('pg')

const connectionLocal = {
  user: 'postgres',
  host: 'localhost',
  database: 'robotworkshop',
  password: 'postgres',
  port: 5432,
}

const connectionProduction = {
  connectionString: process.env.DATABASE_URL, 
  ssl: {rejectUnauthorized: false}
}

const pool = new Pool(process.env.NODE_ENV === 'production' ? connectionProduction : connectionLocal)

module.exports = pool

Creating each API Endpoint

  1. Start with the Router -> Controller -> Model

GET /robots (20 minutes)

//robotRouter.js
const express = require('express')
const router = express.Router()
const {getAllRobots} = require("../controllers/robotsController")

router.get("/", getAllRobots)

module.exports = router
//index.js
const robotRouter = require("./routers/robotRouter")

app.use("/robots", robotRouter)
//robotsController.js
const RobotModel = require("../models/robotModel")

const getAllRobots = async (req, res) => {
  const robots = await RobotModel.getRobotsFromDB()
  res.send(robots)
}

module.exports = {
  getAllRobots
}
//robotModel.js
const pool = require("../db")

class RobotModel {
  static async getAllRobotsFromDB(){
    const query = await pool.query("SELECT * FROM robots;")
    return query.rows
  }
}

module.exports = RobotModel

GET /users/:id (20 minutes)

//userRouter.js
const express = require('express')
const router = express.Router()
const {getSingleUser} = require("../controllers/usersController")

router.get("/:id", getSingleUser)

module.exports = router
//index.js
const robotRouter = require("./routers/robotRouter")
const userRouter = require("./routers/userRouter")

app.use("/robots", robotRouter)
app.use("/users", userRouter)
//usersController.js
const UserModel = require("../models/userModel")

const getSingleUser = async (req, res) => {
  const userID = req.params.id
  const user = await UserModel.getSingleUserFromDB(userID)
  const userRobots = await UserModel.getUsersRobotsFromDB(userID)
  res.send({user, userRobots})
}

module.exports = {
  getSingleUser
}
//userModel.js
const pool = require("../db")

class UserModel { 
  static async getSingleUserFromDB(id){
    let query = await pool.query("SELECT * FROM users WHERE id = $1", [id])
    return query.rows
  }

  static async getUsersRobotsFromDB(id){
    let query = await pool.query("SELECT * FROM user_robots JOIN robots ON user_robots.robot_id = robots.id WHERE user_id = $1", [id])
    return query.rows
  }
}

module.exports = UserModel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment