Meteor - Assets


Advertisements

Static server assets are located in a private subfolder inside the app. In the following example, we will learn how to use data from a simple JSON file.

Step 1 - Create Files and Folders

Let's create a private folder and my-json.json file inside that folder. We will do this using the following command in the command prompt window, however, you can also create it manually.

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

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

Step 2 - Get Text

To be able to read data from our file, we will use Asssets.getText method. Note, this can only be done from the server side. Since we are using JSON, we need to parse it.

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

Following will be the output in the command prompt window.

Meteor Assets Get Text

Step 3 - Create EJSON File

We will create this file inside the private folder. This file will contain binary data "myBinary": {"$binary": "c3VyZS4="}

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

Step 4 - Get Binary

To read EJSON files, we can use the Assets.getBinary method.

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

The command prompt will log EJSON value.

Meteor Assets Get Binary
Advertisements