Meteor - 資源



靜態伺服器資源位於應用程式內的專用子資料夾中。在以下示例中,我們將學習如何從簡單的 JSON 檔案中使用資料。

步驟 1 - 建立檔案和資料夾

讓我們在該資料夾內建立一個專用資料夾和my-json.json檔案。我們將使用命令提示符視窗中的以下命令來執行此操作,但也可以手動建立它。

C:\Users\username\Desktop\meteorApp>mkdir private

C:\Users\username\Desktop\meteorApp\private>touch my-json.json

步驟 2 - 獲取文字

為了能夠從我們的檔案中讀取資料,我們將使用Asssets.getText方法。請注意,這隻適用於伺服器端。我們使用 JSON,因此需要解析它。

if (Meteor.isServer) {
   var myFile = JSON.parse(Assets.getText('my-json.json'));
   console.log(myFile.data.text)
}

以下是命令提示符視窗中的輸出。

Meteor Assets Get Text

步驟 3 - 建立 EJSON 檔案

我們將在專用資料夾中建立此檔案。此檔案將包含二進位制資料“myBinary”: {“$binary”: “c3VyZS4=”}

C:\Users\username\Desktop\meteorApp\private>touch my-ejson.ejson

步驟 4 - 獲取二進位制

若要讀取 EJSON 檔案,我們可以使用Assets.getBinary方法。

if (Meteor.isServer) {
   var myFile = Assets.getBinary('my-ejson.ejson');
   console.log(EJSON.stringify(myFile));
}

命令提示符會記錄 EJSON 值。

Meteor Assets Get Binary
廣告
© . All rights reserved.