This repository has been archived by the owner on Sep 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
serve.js
97 lines (79 loc) · 2.95 KB
/
serve.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
85
86
87
88
89
90
91
92
93
94
95
96
97
const express = require('express');
const app = express();
const port = process.env.PORT || 2020;
app.use(express.static('public'));
app.use('/push', require('./serve-push'));
app.disable('x-powered-by');
app.get('/appcache/appcache.manifest', function(req, res) {
res.set('Content-type', 'text/cache-manifest').status(200).send();
});
const { lstatSync, readdirSync } = require('fs');
const { join } = require('path');
const isDirectory = source => lstatSync(source).isDirectory();
const getDirectories = source =>
readdirSync(source).map(name => name.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'))//.filter(isDirectory).map(name => name);
const dirs = getDirectories('public').join('|');
// const files = getDirectories('public-common');
// for (file of files) {
// app.get(getRegExp(file), function (req, res) {
// res.sendFile()
// });
// }
function getRegExp(path) {
return new RegExp(`/(${dirs})/${path}`);
}
app.get(getRegExp('weather.json'), async function(req, res) {
let temp;
let description;
let details;
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function pad(num) {
return (num < 10 ? '0' : '') + num;
}
weatherTypes = [
'Mostly Sunny',
'Partly Sunny',
'Mostly Cloudy',
'Partly Cloudy',
'Rain',
'Snow'
];
temp = getRandomInt(0, 99);
description = weatherTypes[getRandomInt(0, weatherTypes.length - 1)];
details = [
{label: 'Feels Like', value: getRandomInt(temp - 10, temp + 10) + '°'},
{label: 'Wind', value: getRandomInt(0, 35) + ' mph'},
{label: 'Barometer', value: getRandomInt(29, 31) + '.' + pad(getRandomInt(0, 99)) + ' in'},
{label: 'Visibility', value: getRandomInt(1, 50) + ' mi'},
{label: 'Humidity', value: getRandomInt(20, 90) + '%'},
{label: 'Dew Point', value: getRandomInt(temp - 20, temp - 5) + '°'}
];
setTimeout(function() {
res.status(200).send({
temp: temp,
description: description,
details: details,
time: Date.now()
});
}, 2000);
});
app.get('/appcache(/index.html)?', function(req, res) {
res.sendFile(__dirname + '/public/' + req.params[1] + '/index.html');
});
app.get(getRegExp('manifest.json'), function(req, res) {
res.sendFile(__dirname + '/public-common/manifest.json');
});
app.get(getRegExp('fonts/(.*)'), function(req, res) {
res.sendFile(__dirname + '/public-common/fonts/' + req.params[1]);
});
app.get(getRegExp('images/(.*)'), function(req, res) {
res.sendFile(__dirname + '/public-common/images/' + req.params[1]);
});
app.get(getRegExp('(index.html|bg.jpg|fallback.html|styles.css|common.js|util.js)?'), function(req, res) {
res.sendFile(__dirname + '/public-common/' + (req.params[1] === undefined ? 'index.html' : req.params[1]));
});
app.listen(port, function() {
console.log('Server listening on port ' + port);
});