Skip to content

Instantly share code, notes, and snippets.

@Utopiah
Last active April 20, 2021 17:25
Show Gist options
  • Save Utopiah/34e6ce90cbeecb0bce1d89474d770d28 to your computer and use it in GitHub Desktop.
Save Utopiah/34e6ce90cbeecb0bce1d89474d770d28 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const http = require('http');
const https = require('https');
const express = require('express');
const { ExpressPeerServer } = require('peer')
const app = express()
const domain = 'hack-webar.net' // replace here with your own domain
const privateKey = fs.readFileSync(`/etc/letsencrypt/live/${domain}/privkey.pem`, 'utf8')
const certificate = fs.readFileSync(`/etc/letsencrypt/live/${domain}/fullchain.pem`, 'utf8')
const credentials = {key: privateKey, cert: certificate}
const port = 80
const html = `
<script src="https://unpkg.com/[email protected]/dist/peerjs.min.js"></script>
<script>
const peer = new Peer('someid', {
host: '${domain}',
port: ${port},
path: '/peerjs/myapp',
secure: true,
debug: 3
});
</script>
`
app.get('/', (req, res, next) => res.send(html))
const server = https.createServer(credentials, app);
const peerServer = ExpressPeerServer(server, {
debug: true,
path: '/myapp',
ssl: credentials
});
app.use('/peerjs', peerServer);
server.listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment