1
0
Fork 0

v0.4.2-rc2 (improved middleware error handling)

2015
joates 10 years ago
parent 4dbc0706f2
commit e188723c9a

@ -1,6 +1,6 @@
{ {
"name": "squatconf-web", "name": "squatconf-web",
"version": "0.4.1", "version": "0.4.2",
"description": "website for the squatConf conference", "description": "website for the squatConf conference",
"main": "server.js", "main": "server.js",
"scripts": { "scripts": {

@ -18,6 +18,12 @@ if(!fs.existsSync(config.db_path)){
}); });
} }
stack.errorHandler = function error(req, res, err) {
res.statusCode = 302
res.setHeader('Location', '/')
res.end()
}
var app = stack( var app = stack(
route('/email', require('./src/email-submit')(db)) route('/email', require('./src/email-submit')(db))
, route('/confirm', require('./src/email-confirm')(db)) , route('/confirm', require('./src/email-confirm')(db))

@ -5,6 +5,11 @@ module.exports = function(db) {
return function (req, res, next) { return function (req, res, next) {
req.resume() req.resume()
function error(e) {
console.error(e)
return next(e || e.msg || 'ERROR')
}
var params = require('url').parse(req.url, true) var params = require('url').parse(req.url, true)
if (params && params.query.email && params.query.token) { if (params && params.query.email && params.query.token) {
@ -13,25 +18,30 @@ module.exports = function(db) {
, token = sanitize(params.query.token) , token = sanitize(params.query.token)
db.get(email, function(err, obj) { db.get(email, function(err, obj) {
if (err) return console.error(err) if (err) return error(err)
// db read OK.. // db read OK..
if (obj && obj.token === token) { if (obj && ! obj.verified) {
obj.verified = true if (obj.token === token) {
obj.trace = obj.trace.concat(ip(req)) obj.verified = true
obj.trace = obj.trace.concat(ip(req))
db.put(email, obj, function(err) {
if (err) return console.error(err) db.put(email, obj, function(err) {
if (err) return error(err)
// db write OK..
res.statusCode = 302 // db write OK..
res.setHeader('Location', '/verified.html') res.statusCode = 302
return res.end() res.setHeader('Location', '/verified.html')
}) return res.end()
})
}
} else {
error('email is already verified: '+ email)
} }
}) })
if (next) return next() } else {
error('invalid input: '+ JSON.stringify(params.query))
} }
} }
} }

@ -7,6 +7,11 @@ module.exports = function(db) {
return function (req, res, next) { return function (req, res, next) {
req.resume() req.resume()
function error(e) {
console.error(e)
return next(e || e.msg || 'ERROR')
}
var params = require('url').parse(req.url, true) var params = require('url').parse(req.url, true)
if (params && params.query.email) { if (params && params.query.email) {
@ -22,7 +27,7 @@ module.exports = function(db) {
obj.trace = ip(req) obj.trace = ip(req)
db.put(email, obj, function(err) { db.put(email, obj, function(err) {
if (err) return console.error(err) if (err) return error(err)
// db write OK.. // db write OK..
var nodemailer = require('nodemailer') var nodemailer = require('nodemailer')
@ -39,7 +44,7 @@ module.exports = function(db) {
} }
transporter.sendMail(opts, function(err, data) { transporter.sendMail(opts, function(err, data) {
if (err) return console.error(err) if (err) return error(err)
// validation email sent // validation email sent
console.log('email sent..', opts) console.log('email sent..', opts)
@ -49,9 +54,9 @@ module.exports = function(db) {
return res.end() return res.end()
}) })
}) })
} else {
error('invalid input: '+ JSON.stringify(params.query))
} }
if (next) return next()
} }
} }

Loading…
Cancel
Save