v0.4.2-rc2 (improved middleware error handling)

pull/8/head
joates 10 years ago
parent 4dbc0706f2
commit e188723c9a

@ -1,6 +1,6 @@
{
"name": "squatconf-web",
"version": "0.4.1",
"version": "0.4.2",
"description": "website for the squatConf conference",
"main": "server.js",
"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(
route('/email', require('./src/email-submit')(db))
, route('/confirm', require('./src/email-confirm')(db))

@ -5,6 +5,11 @@ module.exports = function(db) {
return function (req, res, next) {
req.resume()
function error(e) {
console.error(e)
return next(e || e.msg || 'ERROR')
}
var params = require('url').parse(req.url, true)
if (params && params.query.email && params.query.token) {
@ -13,25 +18,30 @@ module.exports = function(db) {
, token = sanitize(params.query.token)
db.get(email, function(err, obj) {
if (err) return console.error(err)
if (err) return error(err)
// db read OK..
if (obj && obj.token === token) {
obj.verified = true
obj.trace = obj.trace.concat(ip(req))
db.put(email, obj, function(err) {
if (err) return console.error(err)
// db write OK..
res.statusCode = 302
res.setHeader('Location', '/verified.html')
return res.end()
})
if (obj && ! obj.verified) {
if (obj.token === token) {
obj.verified = true
obj.trace = obj.trace.concat(ip(req))
db.put(email, obj, function(err) {
if (err) return error(err)
// db write OK..
res.statusCode = 302
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) {
req.resume()
function error(e) {
console.error(e)
return next(e || e.msg || 'ERROR')
}
var params = require('url').parse(req.url, true)
if (params && params.query.email) {
@ -22,7 +27,7 @@ module.exports = function(db) {
obj.trace = ip(req)
db.put(email, obj, function(err) {
if (err) return console.error(err)
if (err) return error(err)
// db write OK..
var nodemailer = require('nodemailer')
@ -39,7 +44,7 @@ module.exports = function(db) {
}
transporter.sendMail(opts, function(err, data) {
if (err) return console.error(err)
if (err) return error(err)
// validation email sent
console.log('email sent..', opts)
@ -49,9 +54,9 @@ module.exports = function(db) {
return res.end()
})
})
} else {
error('invalid input: '+ JSON.stringify(params.query))
}
if (next) return next()
}
}

Loading…
Cancel
Save