-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (50 loc) · 1.54 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// https://github.com/lukeed/polka
// https://dev.to/kvng_zeez/introducing-polka-a-micro-web-server-3p55
// https://www.npmjs.com/package/polka
//import "./test";
//import "./test";
//import {TextImportCall} from './test';
//TextImportCall();
console.log("Polka Server Init...")
//console.log(__dirname);
const polka = require('polka');
const sirv = require('sirv');
const compression = require('compression');
//const uuidv4 = require('uuid/v4');
//const helmet = require('helmet');
const Gun = require('gun');
//const Rad = require('gun/lib/radisk');
//require('gun/sea');
//if you did not config env it will null default
var { PORT , NODE_ENV } = process.env;
const dev = NODE_ENV === 'development';
if(PORT == null){PORT = process.env.PORT || 8080;}
const app = polka();
app.use(compression({ threshold: 0 }));
app.use(sirv('public', { dev }));
app.use(Gun.serve);
//console.log("PORT: "+PORT);
//app.listen(PORT); //this will return polka
app.listen(PORT, err => {
if (err) throw err;
//console.log(app);
console.log(`> Running on localhost:`+PORT);
});
var gunconfig = {
file: 'data',
web:app.server //server
};
var gun = Gun(gunconfig);
gun.on('hi', peer => {//peer connect
//console.log('connect peer to',peer);
console.log('peer connect!');
});
gun.on('bye', (peer)=>{// peer disconnect
//console.log('disconnected from', peer);
console.log('disconnected from peer!');
});
//gun.on('auth', (ack)=>{// peer disconnect
//console.log(ack);
//console.log('disconnected from', peer);
//console.log('disconnected from peer!');
//});