Last active
October 16, 2020 15:13
-
-
Save ThinkingJoules/170b6175e60002e90544589d709f46c7 to your computer and use it in GitHub Desktop.
Basic express + Gun setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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
This is awesome, thank you!