Skip to content

Instantly share code, notes, and snippets.

@mikoloism
Last active June 4, 2021 15:57
Show Gist options
  • Save mikoloism/17a163fad45d66f15f0f06d614e43548 to your computer and use it in GitHub Desktop.
Save mikoloism/17a163fad45d66f15f0f06d614e43548 to your computer and use it in GitHub Desktop.
my mind about how pinterest router work (with express-js)

Pinterest API router/controller

my idea about how pintreset collection/boards api router work

i'm using express-js

Coding

ES6 (babel)

// `/username/board/section`
import { Router } from 'express';

const router = Router();
router.get('/:username/:board/:section', (req, res) => {
  const { username, board, section } = req.params;
  let resualt = [];
  if(username){
    let user = User.find({ username });
    let boards = Board.find({ username });
    
    // if user position on /collection/boards
    if(board){
      let brd = boards.filter(b => b.name === board);
      
      if(brd !== -1){
        if(section){
          let pins = Pins.find({ user, board: brd, section });
          return res.json({ resualt: pins });
        }
        
        let sections = Section.find({ user, board: brd });
        return res.json({ resualt: sections });
      }
    }
    return res.json({ resualt: boards })
  }
  return res.json({ error: 'user-not-found' })
});

ES5 (commonJS)

// `/username/board/section`
const router = require('express').Router();

router.get('/:username/:board/:section', (req, res) => {
  const { username, board, section } = req.params;
  let resualt = [];
  if(username){
    let user = User.find({ username });
    let boards = Board.find({ username });
    
    if(board){
      let brd = boards.filter(b => b.name === board);
      
      if(brd !== -1){
        if(section){
          let pins = Pins.find({ user, board: brd, section });
          return res.json({ resualt: pins });
        }
        
        let sections = Section.find({ user, board: brd });
        return res.json({ resualt: sections });
      }
    }
    return res.json({ resualt: boards })
  }
  return res.json({ error: 'user-not-found' })
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment