-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
62 lines (50 loc) · 1.64 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
58
59
60
61
62
const express = require('express'),
hbs = require('hbs'),
fs = require('fs'),
port = process.env.PORT || 3000
var app = express()
hbs.registerPartials(__dirname + '/views/partials')
app.set('view engine', 'hbs')
app.use((req, res, next) => {
var now = new Date().toString()
var log = `${now}: ${req.method} ${req.url}`
console.log(log)
fs.appendFile('server.log', log + '\n')
next()
})
app.use(express.static(__dirname + '/public'))
// NOTE: The conditional logic on the colors and text etc. could probably be moved
// to the template itself.
function renderPage (res, saturday) {
res.render('saturday.hbs', {
pageTitle: 'Is it saturday?',
welcomeMessage: 'let\'s find out',
saturday: saturday ? 'YES!' : 'NO!',
video: saturday ?
'https://my.mixtape.moe/ymiizt.mp4' : 'https://my.mixtape.moe/kqpane.mp4',
preview: saturday ?
'http://i.imgur.com/o9fmi5P.png' : 'http://i.imgur.com/DNMyePH.png',
color: saturday ? 'green' : 'red'
})
}
const isItSaturday = () => {
const calculateTimeAtOffset = (offset) => {
var d = new Date()
// convert to msec
// add local time zone offset
// get UTC time in msec
var utc = d.getTime() + (d.getTimezoneOffset() * 60000)
// create new Date object sing supplied offset
return new Date(utc + (3600000 * offset))
}
// Saturday in />p/ is defined as Saturday in all timezones between
// UTC-12 and UTC+14.
return calculateTimeAtOffset(-12).getDay() === 6 ||
calculateTimeAtOffset(14).getDay() === 6
}
app.get('/', (req, res) => {
renderPage (res, isItSaturday())
})
app.listen(port, () => {
console.log(`Server is up on port ${port}`)
})