Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chebyte/889e94fe7acbf59e994eb4a2d7f79a4d to your computer and use it in GitHub Desktop.
Save chebyte/889e94fe7acbf59e994eb4a2d7f79a4d to your computer and use it in GitHub Desktop.
Basic express + Gun setup
const path = require('path');
const express = require('express');
const Gun = require('gun');
require('gun/nts')
const port = (process.env.PORT || 8080);
const app = express();
app.use(Gun.serve);
const server = app.listen(port);
var gun = Gun({
web: server,
localStorage: false,
radisk: false //will not save anything to disk on the server
});
global.Gun = Gun; /// make global to `node --inspect` - debug only
global.gun = gun;
setInterval(peers,5000)
function peers(){//this is if you want to know how many peers are connected to your server.
console.log('Peers: '+ Object.keys(gun._.opt.peers).join(', '))
}
const INDEX_HTML = path.join(__dirname,'public/index.html')
if (process.env.NODE_ENV !== 'production') {//you will have to figure out how this works for your app.
app.use(express.static('public'));
app.get('*', function (_, res) {
res.sendFile(INDEX_HTML);
});
}else{
const indexPath = path.join(__dirname, 'dist/index.html');
app.use(express.static('dist'));
app.get('*', function (_, res) {
res.sendFile(indexPath);
});
}
"scripts": {
"server": "node server.js",
"start": "npm run server"
},
"dependencies": {
"express": "^4.16.4",
"gun": "^0.2019.612",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment