Skip to content

Instantly share code, notes, and snippets.

@ThinkingJoules
Last active October 16, 2020 15:13
Show Gist options
  • Save ThinkingJoules/170b6175e60002e90544589d709f46c7 to your computer and use it in GitHub Desktop.
Save ThinkingJoules/170b6175e60002e90544589d709f46c7 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",
}
@Dletta
Copy link

Dletta commented Jun 18, 2019

This is awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment