Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var express = require('express')
var app = express()
var pug = require('pug')
var fs = require('fs')
var bp = require('body-parser')
const config = './config.json'
const timeout = 300 // 5mins
app.use(bp.urlencoded({ extended: false }))
app.use(bp.json())
app.get('/', function (req, res) {
let raw = fs.readFileSync(config, 'utf8')
let c = JSON.parse(raw)
let d = Math.floor(Date.now() / 1000) - timeout
for( var index in c.members ){
if( c.pwd[index] && c.pwd[index].ts >= d ){
c.members[index].color = "green"
}else{
c.members[index].color = "red"
}
}
res.send(pug.renderFile('templates/main.pug', {
members: c.members
}))
})
app.get('/submit', function (req, res) {
let raw = fs.readFileSync(config, 'utf8')
let c = JSON.parse(raw)
let d = Math.floor(Date.now() / 1000) - timeout
for( var index in c.members ){
if( c.pwd[index] && c.pwd[index].ts >= d ){
continue
}
res.send(pug.renderFile('templates/verify-fail.pug'))
return
}
fs.readFile(c.voucher.path, function (err,data){
res.contentType("application/pdf")
res.send(data)
})
})
app.get('/:name', function (req, res) {
let raw = fs.readFileSync(config, 'utf8')
let c = JSON.parse(raw)
let m = req.params.name
res.send(pug.renderFile('templates/member.pug', {
member: c.members[m],
id: m
}))
})
app.post('/:name/submit', function (req, res) {
let raw = fs.readFileSync(config, 'utf8')
let c = JSON.parse(raw)
let user = req.params.name
let pass = req.body.password
if ( pass === c.pwd[user].pwd ) {
c.pwd[user].ts = Math.floor(Date.now() / 1000)
fs.writeFileSync(config, JSON.stringify(c), 'utf8')
res.send(pug.renderFile('templates/submit-ok.pug'))
}else{
res.send(pug.renderFile('templates/submit-fail.pug'))
}
})
app.listen(8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.