1. 程式人生 > >搭建微信小程式的本地測試伺服器 json-server

搭建微信小程式的本地測試伺服器 json-server

1.首先需要在windows環境下安裝node.js

   選擇 Windows Installer 下載對應的系統版本就行,然後一路next,這種方式安裝好以後會把環境變數也配置好了,直接在命令列下輸入

node --version      //出現對應的版本號資訊就表示安裝成功了

2.json-server的使用

<1> 安裝:npm install -g json-server

<2>然後選一個檔案目錄新建一個json檔案,例如index.json

 

index.json 內容

{
  "data": [
    {
      "img": "/images/pro_01.jpg",
      "title": "test",
      "desc": "這是個測試1"
    },
    {
      "img": "/images/pro_02.jpg",
      "title": "test",
      "desc": "這是個測試2"
    },
    {
      "img": "/images/pro_03.jpg",
      "title": "test",
      "desc": "這是個測試3"
    },
    {
      "img": "/images/pro_01.jpg",
      "title": "test",
      "desc": "這是個測試4"
    }
  ]
}

 

<3> 啟動json-server服務:在新建的json資料夾內,執行命令:json-server + 檔名  (先cd 到對應的資料夾內)

  • 瀏覽器中輸入地址http://localhost:3000/data就能看到輸出index.json的內容
  • 至此,就搭建了一個簡單的本地測試伺服器,json-server支援get,post等,基本足夠開發測試用了,具體的可以參考json-server官網