-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
84 lines (68 loc) · 2.19 KB
/
index.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
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
const express = require('express');
const path = require('path');
const generators = require('./generators');
const config = require('./config');
const demos = require("./demos.json");
const app = express();
//public route
app.use(express.static('public'));
//create demo routes
demos.forEach(demo => {
app.get(`/${demo.name}`, function (req, res) {
res.sendFile(path.resolve(`public/${demo.name}.html`));
});
});
//service worker generator
app.get('/:demo/sw.js', function(req, res){
const demo = req.params.demo;
const data = demos.find(d => {
return d.name === demo;
});
const template = `
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open('v1').then((cache) => {
return cache.addAll([
${data.cacheList}
]);
})
);
});
self.addEventListener('fetch', function(event) {
// Calling event.respondWith means we're in charge
// of providing the response. We pass in a promise
// that resolves with a response object
event.respondWith(
// First we look for something in the caches that
// matches the request
caches.match(event.request).then(function(response) {
// If we get something, we return it, otherwise
// it's null, and we'll pass the request to
// fetch, which will use the network.
return response || fetch(event.request);
})
);
});
`;
res.status(200).type("text/javascript").send(template);
});
//create routes for manifests
//OpenFin manifest
app.get('/manifests/of/:demo', function(req, res){
const demo = req.params.demo;
const data = demos.find(d => {
return d.name === demo;
});
res.status(200).json(generators.openfin.generate(data,req));
});
//web app manifest
app.get('/manifests/web/:demo', function(req, res){
const demo = req.params.demo;
const data = demos.find(d => {
return d.name === demo;
});
res.status(200).json(generators.web.generate(data,req));
});
app.listen(config.port, () => {
console.log(`App running on port ${config.port}.`);
});