3

When a js-ipfs node is started programatically using the below code in node.js app, it is launching the swarm, allowing to add files and query them back.

// code from the docs: https://github.com/ipfs/js-ipfs#use-in-nodejs

const IPFS = require('ipfs')
const node = new IPFS()

node.on('ready', () => {
  // Ready to use!
})

But the API and gateway are not available, which means the web-ui is not available to inspect the repo contents. How to start the API gateway along with ipfs swarm using the ipfs npm package?

Gopalakrishna Palem
  • 1,705
  • 1
  • 20
  • 34

2 Answers2

5

Found the answer, posting here to help anyone looking for similar info.

The API gateway is available as http module inside the ipfs, which can be invoked as shown below on start of the ipfs node:

const IPFS = require('ipfs')
const node = new IPFS()

node.on('ready', () => {
   // start the API gateway
    const Gateway = require('ipfs/src/http');
    const gateway = new Gateway(node);
    return gateway.start();
})

The API and Gateway will be listening on the ports specified in the config that is used in the new IPFS(), which can be edited from the repo/config file location or supplied programatically, such as:

  "Addresses": {
    "API": "/ip4/127.0.0.1/tcp/5001",
    "Gateway": "/ip4/127.0.0.1/tcp/8080"
  }
Gopalakrishna Palem
  • 1,705
  • 1
  • 20
  • 34
  • 1
    This is correct, however, we don't recommend requiring internals directly (it is an antipattern). Instead, check out https://github.com/ipfs/js-ipfsd-ctl/, it lets you spawn all kinds of daemons and nodes :) – David Dias May 22 '19 at 06:55
  • 3
    thank you @DavidDias The `ipfsd-ctl` looks more like spwaning a whole different process altogether. If we have node started already with the `ipfs` package (as shown in my code above), would the gateway started with the `ipfsd-ctl` be still interacting with the node that is already started, or spawns a brand-new node and gateway combo together. More importantly, the reason for starting a node with `ipfs` package is to control the `datastore` (with customized repo functionality). I am not sure if `ipfsd-ctl` would be providing the gateway for my node (with custom datastore node) in such cases. – Gopalakrishna Palem May 22 '19 at 14:35
2

TCP ports with HTTP API and Gateway are opened when running js-ipfs as a daemon (Node.js):

$ jsipfs daemon
(...)
Gateway (read only) listening on /ip4/127.0.0.1/tcp/9090/http
Web UI available at http://127.0.0.1:5002/webui
Daemon is ready

JavaScript running in regular web browser is unable to open TCP ports so js-ipfs running on a web page does not expose HTTP API and Gateway.

You need to use programmatic interface to interact with it.

lidel
  • 525
  • 3
  • 7
  • 1
    Thank you @lidel . The question was more about programmatic use of `ipfs` package than CLI. Sorry about the confusion. Added more clarity to the question. Also, found an answer and posted it below. Thank you . – Gopalakrishna Palem May 22 '19 at 02:08