D38_W4 HW2 + HW3


Posted by Christy on 2021-05-26

W4 HW2

const request = require('request');
const process = require('process')

const action = process.argv[2]

if (action === 'list') {
  listBook()
} else if (action === 'read') {
  readBook(process.argv[3])
} else if (action === 'delete') {
  deleteBook(process.argv[3])
} else if (action === 'create') {
  createBook(process.argv[3])
} else if (action === 'update') {
  updateBook(process.argv[3], process.argv[4])
} else {
  console.log('error')
}

function listBook() {
  request('https://lidemy-book-store.herokuapp.com/books?_limit=20', 
    function (error, response, body) {
      if (error) {
        console.log('error:', error)
        return 
      }

      let obj 
      try {
        obj = JSON.parse(body)
      } catch(error) {
        console.log(error)
        return
      }
      for (let i = 0; i < obj.length; i++) {
        console.log(`${obj[i].id} ${obj[i].name}`)
      }    
    });
}

function readBook(id) {
  request('https://lidemy-book-store.herokuapp.com/books/' + id, 
    function (error, response, body) {
      if (error) {
        console.log('error:', error)
        return 
      }

      let book 
      try {
        book = JSON.parse(body)
      } catch(error) {
        console.log(error)
        return
      }
      console.log(book)
    });
}

function deleteBook(id) {
  request.delete('https://lidemy-book-store.herokuapp.com/books/' + id, 
  function (error, response, body) {
    if (error) {
      console.log('error:', error)
      return 
    }

    let book 
    try {
      book = JSON.parse(body)
    } catch(error) {
      console.log(error)
      return
    }
    console.log(book)
  });
}

function createBook(name) {
  request.post({url:'https://lidemy-book-store.herokuapp.com/books/', 
    form: {
      id: '',
      name: process.argv[3]
    }
  }, 
    function(error, response, body) {
      if (error) {
      console.log('error:', error)
      return 
    }

    let book 
    try {
      book = JSON.parse(body)
    } catch(error) {
      console.log(error)
      return
    }
    console.log(book)
    }
  )
}

function updateBook(id, name) {
  request.patch({url:'https://lidemy-book-store.herokuapp.com/books/' + id, 
    form: {
      name
    }
  }, 

  function (error, response, body) {
    if (error) {
      console.log('error:', error)
      return 
    }

    let book 
    try {
      book = JSON.parse(body)
    } catch(error) {
      console.log(error)
      return
    }
    console.log(book)
  });
}
  • 學習心得:
  1. 在新增書籍 form 表單試了一下,原來是用這個啊,一個蘿蔔一個坑,自己試出來很爽

     form: {
           id: '', // 一直忘記逗號,哼
           name: process.argv[3]
         }
    
  2. 串 API 重點是看懂題目跟 API 文件,知道哪個功能要怎麼用,例如說新增書籍,一開始會不知道跟 forms 有什麼關聯性

  3. 原來更新書籍就是把名字直接給它。然後我還很蠢的一直用 node 123.js patch 1 "yoyoyo" 是 update 不是 patch 啊!

  4. 我終於把作業二搞懂了,忽然有個想法,如果下次現在的我要教未來已經忘記作業二的 API 怎麼串的我,我該怎麼做呢?

    首先,還是要先把所有影片都掃一遍,接著看那兩篇重要的文章。

    實作的部分,我會跟未來的我解釋,所謂串 API 有一個類似模板的東西,最上面會有 API 相關套件的內建功能(例如 request, process 之類的,這些基本功能);接著釐清要實作的功能,再把 API 文件好好讀懂,這樣可能會更快喚起我的記憶。API 文件應該以後很常遇到的話,多看幾個版本就會更快上手了吧?

    所謂模板大概長這樣,第七行會是重點,功能要寫在那:

    request.patch, url 後面加 id 之類的

    但是寫完這週作業,應該會需要把串 API 的程式,朝更簡潔的語法邁進,不過那是之後的事了,先把功能寫出來再說。

const request = require('request');
const process = require('process')

const action = process.argv[2]

const request = require('request');
request('http://www.google.com', function (error, response, body) {
   if (error) {
    console.log('error')
  }

  let book
  try {
    book = JSON.parse(body)
  } catch (error) {
    console.log('error')
    return
  } 

  console.log('body:', body);
});

W4 HW3 還在嘗試當中










Related Posts

七天打造自己的 Google Map 應用入門 - Day03

七天打造自己的 Google Map 應用入門 - Day03

[Day 1] JS in Pipeline - DevOps for Local Development Environment (1)

[Day 1] JS in Pipeline - DevOps for Local Development Environment (1)

W14 直播檢討

W14 直播檢討


Comments