Js-ipfs: Clone `ipfs daemon` in node

Created on 22 Jul 2017  路  11Comments  路  Source: ipfs/js-ipfs

I'm trying to recreated the command ipfs daemon in node.

const IPFS = require('ipfs')
// Create the IPFS node instance
const node = new IPFS({
  config: { // overload the default config
    API: { // API config for IPFS daemon
      HTTPHeaders: {
        "Access-Control-Allow-Origin": ["*"], // Origins from which to allow http requests
        "Access-Control-Allow-Methods": ["PUT", "GET", "POST"], // "PUT", "GET", "POST", "DELETE", etc.
        "Access-Control-Allow-Credentials": ["true"] // "true" || "false"
      }
    }
  }
})

node.on('ready', () => {

})

But in the browser I''m getting fetch errors

request.js:132 POST http://localhost:5001/api/v0/add?stream-channels=true net::ERR_CONNECTION_REFUSED
P3 kinquestion statuready

Most helpful comment

If you are looking for a daemon in the sense of a running js-ipfs process that exposes an http-api, then check https://github.com/ipfs/js-ipfs/tree/master/src/http-api

If you look to have a instance in the sense of a in process node that does not expose an http-api, but does expose the core api, then check https://github.com/ipfs/js-ipfs#ipfs-core-use-ipfs-as-a-module

All 11 comments

Thanks for reporting this issue @milesalex. You are trying to create a daemon out of the IPFS instance (core). The daemon needs to instantiated differently. See https://github.com/ipfs/js-ipfs#usage

Were you able to get it to work?

// Create the IPFS node instance
const node = new IPFS({
  repo: repo,
  init: true,
  start: true,
  config: { // overload the default IPFS node config
    Addresses: {
      Swarm: [
        '/ip4/127.0.0.1/tcp/4001'
      ],
      API: '/ip4/127.0.0.1/tcp/5001'
    }
  }
})

This was my attempt.

If you are looking for a daemon in the sense of a running js-ipfs process that exposes an http-api, then check https://github.com/ipfs/js-ipfs/tree/master/src/http-api

If you look to have a instance in the sense of a in process node that does not expose an http-api, but does expose the core api, then check https://github.com/ipfs/js-ipfs#ipfs-core-use-ipfs-as-a-module

I have my client side run like:

const ipfsAPI = require('ipfs-api')
const ipfs = ipfsAPI('/ip4/127.0.0.1/tcp/5001')

And then trying to expose the core api daemon on port 5001 from the backend:

const IPFS = require('ipfs')
const repo = String(Math.random() + Date.now())


// Create the IPFS node instance
const node = new IPFS({
  repo: repo,
  init: true,
  start: true,
  config: { // overload the default IPFS node config
    Addresses: {
      Swarm: [
        '/ip4/127.0.0.1/tcp/4001'
      ],
      API: '/ip4/127.0.0.1/tcp/5002'
    }
  }
})

I am getting that error from console when i try and connect from the front end:
request.js?af32:132 POST http://127.0.0.1:5001/api/v0/add?recursive=true&stream-channels=true net::ERR_CONNECTION_REFUSED

Both frontend and backend are running from the same machine! My Goal is to connect to ipfs, without having to run. ipfs daemon locally.

@426F746368 you are still trying to run an instance and not a daemon.

You expose a daemon, you need to do this steps https://github.com/ipfs/js-ipfs/blob/master/src/http-api/index.js

@diasdavid, appreciated. Will def look at them. You reckon it is better to run a daemon on a local machine, yielding better results than on a nodejs server?

The daemon is a Node.js server :) Hope this answers your question

if you want to connect remote server
change the 127.0.0.1 to 0.0.0.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gitGksgk picture gitGksgk  路  3Comments

daviddias picture daviddias  路  3Comments

daviddias picture daviddias  路  3Comments

daviddias picture daviddias  路  4Comments

Ntmf picture Ntmf  路  4Comments