顯示具有 nodejs 標籤的文章。 顯示所有文章
顯示具有 nodejs 標籤的文章。 顯示所有文章

星期一, 4月 06, 2020

axios catch

一般是寫 .catch((err) => {console.log(err);}
這樣只會看到 http status 的錯誤,不能取得 response。那該怎麼取得 error response 呢?axios 文件有寫了,用 err.response: https://github.com/axios/axios#handling-errors
axios.get('/user/12345')
  .catch(function (error) {
    if (error.response) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx
      console.log(error.response.data);    // 取得內容,用 error.response.data
      console.log(error.response.status);  // 取得狀態碼,用 error.response.status
      console.log(error.response.headers); // 取得表頭,用 error.response.headers
    } else if (error.request) {
      // The request was made but no response was received
      // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
      // http.ClientRequest in node.js
      console.log(error.request);
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);
    }
    console.log(error.config);
  });

星期五, 8月 05, 2016

docker-hackmd relative url

hackmd 是一個很棒的協作平台,你可以用 markdown 來撰寫文件,graphviz/flowchart 等語法來畫圖...很厲害。
安裝上也蠻簡單的,已經有人做好 Dockerfile :hackmdio/docker-hackmd: docker hackmd image
可是這個 docker image 有個問題,就是沒辦法以 relative url 存在,他預設是在根目錄下運作,有個日本人弄出來了:HackMDをnginxで / 以外のlocationで起動する。 - Qiita ,我參考他的設定,做了調整,加入 nginx 設定與 upstart 設定,放在 elleryq/docker-hackmd: docker hackmd image
大致調整以下東西:
  1. nginx 設定:加入 rewrite,將路徑改寫為 /hackmd,這可以參考 nginx.conf.example
  2. common.js: 因為 hackmd 用到 websocket ,common.js 的 urlpath 也要跟著調整,否則會無法運作,裏面的 urlpath 需要修改為 /hackmd。這部份我寫在 hackmd/Dockerfile 裡,在用 git clone 取得 hackmd 原始碼以後,用 sed 去做字串的替換。
  3. upstart:upstart.hackmd.conf 裡是用 docker-compose 啟動 hackmd image ,這邊我預期 docker-compose.yml 是放在 /srv/docker-hackmd ,如果你預期不放在這兒,那麼這邊也要跟著調整。
應該大概就這些,如果有沒提到的,就看原始碼吧~

星期五, 3月 07, 2014

AngularJS 起手式

我是參考這篇教學:Learn to Build Modern Web Apps with the AngularJS Tutorial

教學裡使用的是 yeoman ,這真的有方便。首先要安裝 nodejs 跟 npm,在 Ubuntu 13.10 裡,就用 sudo apt-get install nodejs npm 就行了,如果是 Ubuntu 12.04,得另外裝 chris lea 的 PPA,再 update/install。教學裡的第一步,就是安裝 yeoman,用 sudo npm install -g yo 來安裝,可是我不太喜歡直接裝到 /usr 系統資料夾去,所以找了一下,看能不能像 Python 的 pip install xxx –user 一樣裝到使用者目錄下。

搜尋的結果是可以的,但需要做些設定。設定值大致如下:
這樣就可以不用 sudo,然後用 npm install yo 就可以把相關模組都裝到使用者目錄下了,而且相關的指令也都可以用。

參考資料: