From db307dcd96b432e44c3e3b1fc2ee2842717aad27 Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Wed, 12 Sep 2018 12:11:36 +1000 Subject: [PATCH] Update README.md --- README.md | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f73b105..68035b1 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,6 @@ Available options: | Option | Description | Default | | -----| ------------| ------- | | `gateway` | URL of the Swarm gateway, ie `http://localhost:8500`. | `swarm-gateways.net` | -| `mode` | Protocol of the default gateway URL. If `gateway` is provided, this has no effect. | `https` | NOTE: If no options are provided, the default gateway URL will be `https://swarm-gateways.net`. This means you don't necessarily need to [run your own Swarm node](https://swarm-guide.readthedocs.io/en/latest/gettingstarted.html), however there is an upload limit of ~2.5MB and no guarantees about permanence. It is recommended to run your own Swarm node. ##### Check gateway availability @@ -63,14 +62,35 @@ fs.readFile('./index.html', (err, data) => { // file contents can now be accessed from 'http://localhost:8500/bzz-raw:/178739cbbd084e90ae0cef3f95e4b92baa85e83edb1a52d28dc370277db9d457' ``` ##### Upload directory (only available in NodeJS, not available in the browser!) -Once uploaded, the hash of **the manifest** is returned. Accessing the manifest from `bzz:/` will not return anything. Instead, you can use the hash as the root of your directory and access the individual files by appending them to the end of the URL. So, if we uploaded a directory containing `index.html`, we will be able to access it via `/bzz://index.html`. +Once uploaded, the hash of **the manifest** is returned. Accessing the manifest from `bzz:/` will 404. Instead, you can use the hash as the root of your directory and access the individual files by appending them to the end of the URI, then call `swarmapi download `. +So, if our uploaded directory looked like: +``` +├── dist +│ └── root.html +│ ├── folder +│ │ └── index.js +``` +Run the upload directory command ``` -// upload the dist folder, and for this example, we can assume this folder contains index.html and dapp.css swarm.uploadDirectory('dist/', null, (err, hash) => { if(err) return console.error('Error uploading directory', err); - console.log(`We can now access our directory via 'http://localhost:8500/bzz:/${hash}/index.html' and 'http://localhost:8500/bzz:/${hash}/dapp.css'`); + console.log(`We can now access our directory via 'http://localhost:8500/bzz:/${hash}/root.html' and 'http://localhost:8500/bzz:/${hash}/folder/index.js'`); +}); +// We can now access our directory via 'http://localhost:8500/bzz:/26089099a5f473dfb7b172de6558972989f8db4d3948daedbb974025be7c8534/root.html' and 'http://localhost:8500/bzz:/26089099a5f473dfb7b172de6558972989f8db4d3948daedbb974025be7c8534/folder/index.js' +``` +We will be able to access the `root.html` file via +``` +swarmjs.download('bzz:/26089099a5f473dfb7b172de6558972989f8db4d3948daedbb974025be7c8534/root.html', (err, content) => { + if(err) return console.error(err); + console.log(`contents of root.html: ${content}`); +}); +``` +And the `folder/index.js` file via +``` +swarmjs.download('bzz:/26089099a5f473dfb7b172de6558972989f8db4d3948daedbb974025be7c8534/folder/index.js', (err, content) => { + if(err) return console.error(err); + console.log(`contents of folder/index.js: ${content}`); }); -// We can now access our directory via 'http://localhost:8500/bzz:/26089099a5f473dfb7b172de6558972989f8db4d3948daedbb974025be7c8534/index.html' and 'http://localhost:8500/bzz:/26089099a5f473dfb7b172de6558972989f8db4d3948daedbb974025be7c8534/dapp.css' ``` ##### Download content ``` @@ -108,7 +128,7 @@ swarmapi uploadfile index.html --gateway http://localhost:8500 // [http://localhost:8500] Swarm hash: 91a3e25c9cec6d470eeaff24f2e5b5e56b87482eb289da8041b1f97a3dbbd39f ``` ##### Upload directory (only available in NodeJS, not available in the browser!) -Once uploaded, the hash of **the manifest** is returned. Accessing the manifest from `bzz:/` will 404. Instead, you can use the hash as the root of your directory and access the individual files by appending them to the end of the URL, then call `swarmapi download `. +Once uploaded, the hash of **the manifest** is returned. Accessing the manifest from `bzz:/` will 404. Instead, you can use the hash as the root of your directory and access the individual files by appending them to the end of the URI, then call `swarmapi download `. So, if our uploaded directory looked like: ``` ├── dist @@ -135,4 +155,4 @@ swarmapi download bzz:/06da636c60c4a5472b209c71e35170044e8cf939aca6acfec765514f3 ``` swarmapi downloadraw a294d3a33dc0b4d9a12fdb48f5a7aed5f8dd2d8fc6a4253487c63012210e210e -g http://localhost:8500 // Hello World -``` \ No newline at end of file +```