D41_檢討 W4 作業


Posted by Christy on 2021-05-29

作業三檢討

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

const name = process.argv[2]

// 這裡要用正確的 URL,之前用的不合題意
// 只要裡面有 template literal,都要用兩個反引號包起來
request(`https://restcountries.eu/rest/v2/name/${name}`,
  function (error, response, body) {
    if (error) {
      console.log('error')
      return
    }
    let str 
    try {
      str = JSON.parse(body)
    } catch(error) {
      console.log(error)
      return
    }

    if (str.statusCode === 404) {
      console.log('找不到國家資訊')
      return
    }

    for (let i = 0; i < str.length; i++) {
      // 如果寫了下面這行,那只能搜尋到名字完全符合的國家
      // if(name === str[i].name) {
        console.log('============')
        console.log('國家:' + str[i].name)
        console.log('首都:' + str[i].capital)
        console.log('貨幣:' + str[i].currencies[0].code)
        console.log('國碼:' + str[i].callingCodes[0])
      // } 
    }
})

自我檢測

  • 記錄自我檢測裡面沒辦法立刻說出來的概念:

    P1 你知道什麼是 DNS 以及運作原理
    答:全名叫做 Domain Name System,會回傳 IP 位址,很像電話黃頁可以查詢電話地址。

    參考資料:DNS 伺服器是什麼?如何運用?

P1 你知道 localhost 跟 127.0.0.1 是什麼
答:localhost 是一個網域,每個網域都會對應到一台電腦,每台電腦會有一個 IP 位址;127.0.0.1 是一個特殊的 IP 位址,代表自己的電腦

參考資料:第十七天:怎麼讓別人連到我作好的網站?

 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', 
  (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, 
  (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, 
  (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]
    }
  },
  (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
    }
  },

  (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)
  })
}









Related Posts

Redux 的三大要件與觸發順序

Redux 的三大要件與觸發順序

MTR04_0922

MTR04_0922

Interceptor

Interceptor


Comments