-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
109 lines (96 loc) · 2.76 KB
/
Copy pathserver.js
File metadata and controls
109 lines (96 loc) · 2.76 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var express = require('express');
var uuidv4 = require('uuid/v4');
var app = express ();
var dict = {};
app.set ('view engine', 'hbs');
var id = uuidv4();
var trackID = id;
app.use(express.static(__dirname + "/public/images"));
var manifest = {
"name": "PWA for tracking users",
"short_name": "Tracking users",
"description": "PWA that demonstrates user tracking",
"icons": [
{
"src": '/jslogo.png',
"type": "image/png",
"sizes": "512x512"
},
{
"src": '/jslogo.png',
"type": "image/png",
"sizes": "192x192"
}
],
"start_url": '/?id='+ trackID,
"display": "standalone",
"background_color": "#f0db4f",
"theme_color": "#FFC0CB"
};
app.get ('/', (req, res) => {
var id = req.query.id;
console.log(dict[id]);
if(dict[id]){ // hvis brukeren finnes
dict[id]++;
}else{
dict[id]=1
}
console.log(dict)
res.render ('index.hbs', {trackID: trackID, stats: dict[trackID]});
});
app.get('/:?id=' + trackID, (req, res) => {
res.render ('home.hbs');
});
app.get("/manifest.json", (req, res) => {
res.append("Content-Type", "text/json; charset=utf-8")
res.send(JSON.stringify(manifest))
})
app.get("/app.js", (req, res) => {
res.append("Content-Type", "text/javascript; charset=utf-8")
res.send(`
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register ('/sw.js')
.then (registration => console.log ('Service worker registration successful!' + registration.scope));
}
`)
})
app.get("/sw.js", (req, res) => {
res.append("Content-Type", "text/javascript; charset=utf-8")
res.send(`
var staticCacheName = 'pre-cache';
self.addEventListener('install', function (event) {
console.log('ServiceWorker (' + staticCacheName + '): install called');
event.waitUntil(
caches.open(staticCacheName).then(function (cache) {
return cache.addAll([
'/?id=${trackID}',
'/',
]);
})
);
});
self.addEventListener('activate', function (event) {
console.log('ServiceWorker: Activate');
//activate active worker asap
event.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', function (event) {
//handle live reload function (for develop purpose)
if (event.request.url.indexOf('/browser-sync/') !== -1) {
//fetch(..) is the new XMLHttpRequest
event.respondWith(fetch(event.request));
return;
}
console.log('ServiceWorker: fetch called for ' + event.request.url);
//if request in cache then return it, otherwise fetch it from the network
event.respondWith(
caches.match(event.request).then(function (response) {
return response || fetch(event.request);
})
);
});
`)
})
app.listen(8080, function()
{console.log("server listening")})