|
| 1 | +# Running multiple JS IPFS nodes |
| 2 | + |
| 3 | +This example takes you through the process needed to run 2 or more JS IPFS nodes on the same computer. |
| 4 | + |
| 5 | +## Via the CLI |
| 6 | + |
| 7 | +Firstly, you'll want to use the `IPFS_PATH` env variable to get a different repo for each instance. Initialise a new IPFS repo like this: |
| 8 | + |
| 9 | +```sh |
| 10 | +# IPFS_PATH by default is `~/.jsipfs`. |
| 11 | +# The following instructs JS IPFS to use the path `~/.jsipfs2` instead: |
| 12 | +IPFS_PATH=~/.jsipfs2 jsipfs init |
| 13 | + |
| 14 | +# Repeat this for as many nodes as you want to run... |
| 15 | +``` |
| 16 | + |
| 17 | +Secondly, you'll need them to bind to different ports because otherwise bad things happen. |
| 18 | + |
| 19 | +With the CLI, after you've run `ipfs init` you can either edit the config file at `~/.jsipfs/config` (replacing `~/.jsipfs` with the repo path you specified above) or use the config command to update the config e.g. `ipfs config Addresses.API /ip4/0.0.0.0/tcp/4012`. Then start the node with `ipfs daemon`: |
| 20 | + |
| 21 | +```sh |
| 22 | +# edit the address ports |
| 23 | +vi ~/.jsipfs2/config |
| 24 | + |
| 25 | +# OR |
| 26 | + |
| 27 | +IPFS_PATH=~/.jsipfs2 jsipfs config Addresses.API /ip4/127.0.0.1/tcp/5012 |
| 28 | + |
| 29 | +# Repeat this for as many nodes as you want to run... |
| 30 | +``` |
| 31 | + |
| 32 | +```sh |
| 33 | +# ...and then start the daemon |
| 34 | +IPFS_PATH=~/.jsipfs2 jsipfs daemon |
| 35 | + |
| 36 | +# Repeat this for as many nodes as you want to run... |
| 37 | +``` |
| 38 | + |
| 39 | +## Programmatically |
| 40 | + |
| 41 | +Firstly, you'll want to pass the [`repo`](https://github.com/ipfs/js-ipfs#optionsrepo) option to the constructor to get a different repo for each instance: |
| 42 | + |
| 43 | +```js |
| 44 | +// The repo path by default is `os.homedir() + '/.jsipfs'`. |
| 45 | +new IPFS({ repo: os.homedir() + '/.jsipfs2' }) |
| 46 | +``` |
| 47 | + |
| 48 | +Secondly, you'll need them to bind to different ports because otherwise bad things happen. |
| 49 | + |
| 50 | +To do this, simply pass the different ports to the [`config`](https://github.com/ipfs/js-ipfs#optionsconfig) constructor option. All together: |
| 51 | + |
| 52 | +```js |
| 53 | +new IPFS({ |
| 54 | + repo: os.homedir() + '/.jsipfs2', |
| 55 | + config: { |
| 56 | + Addresses: { |
| 57 | + Swarm: [ |
| 58 | + '/ip4/0.0.0.0/tcp/4012', |
| 59 | + '/ip4/127.0.0.1/tcp/4013/ws' |
| 60 | + ], |
| 61 | + API: '/ip4/127.0.0.1/tcp/5012', |
| 62 | + Gateway: '/ip4/127.0.0.1/tcp/9191' |
| 63 | + } |
| 64 | + } |
| 65 | +}) |
| 66 | +``` |
0 commit comments