guggio
guggio

Reputation: 221

How to create gun server on heroku

I'm reading about gun db. I want to create an electron app that will sync data between two or more clients and I'm thinking to use heroku to run a gun server. I found this example code of how to setup a gun server using node and express:

var port    = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765;
var express = require('express');
var Gun     = require('..');
require('../axe');

var app    = express();
app.use(Gun.serve);
app.use(express.static(__dirname));

var server = app.listen(port);
var gun = Gun({ file: 'data', web: server });

global.Gun = Gun; /// make global to `node --inspect` - debug only
global.gun = gun; /// make global to `node --inspect` - debug only

console.log('Server started on port ' + port + ' with /gun');

I don't know if this will work on heroku, as I know, heroku has it's own port to listen to. Also I found an heroku button to quickly deploy a gun server on heroku. If I use it how I can modify the code pushed on heroku if needed? Anyone has experience with this decentralized database?

Upvotes: 0

Views: 981

Answers (1)

Tin Nguyen
Tin Nguyen

Reputation: 5330

1. Heroku listens to its own port which you need to bind to. It is already done in the code you provided process.env.PORT.

var port    = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765;

It binds to port 8765 if the environment variables OPENSHIFT_NODEJS_PORT, VCAP_APP_PORT and PORT doesn't exist and there wasn't an argument provided when executing the program.

2. Heroku's file system are ephemeral. Anything that is not versioned/committed in git is lost. So using Heroku's filesystem as a database is a bad idea.

https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted

You need a real database server.

3. Provide the Heroku Deploy Button link

You should be able to just fork the project on GitHub, make your changes there. Heroku Deploy Buttons are reusable.

Upvotes: 1

Related Questions