Skip to content

Instantly share code, notes, and snippets.

@fiinom
Created March 14, 2012 05:58
Show Gist options
  • Save fiinom/2034415 to your computer and use it in GitHub Desktop.
Save fiinom/2034415 to your computer and use it in GitHub Desktop.
Kickoff labs from node.js
const models = require('../models');
const http = require('http');
const querystring = require('querystring');
exports.index = function(req, res, next){
console.log("isAdmin: ", req.session["isAdmin"]);
res.render('application/index', {
auth: req.getAuthDetails(),
session: req.session
});
};
exports.login = function(req, res, next){
res.render('application/login');
};
exports.betaSignup = function(req, res, next){
var email = req.body.email;
models.User.where('email', email, function(err, users){
if (err){
console.error(500);
return res.send(500);
}
// Invoke kickofflabs.com REST api for registering a user
// 1799 is the Fiinom API key for kickofflabs.
if (users.length){
var postdata = querystring.stringify({
'email' : email,
'social_id' : '',
'skipAR' : '0'
});
var options = {
host: 'api.kickofflabs.com',
port: 80,
path: '/v1/1799/subscribe',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postdata.length
}
}
var req = http.request(options, function(res){
console.log('--- kickofflabs api call --')
console.log('postdata: ' + postdata);
console.log('STATUS: ' + res.statusCode);
res.setEncoding('utf8');
res.on('data', function(chunk){
console.log('BODY: '+ chunk);
});
});
req.write(postdata);
req.end();
}
if (users.length){
console.log("email %j already exists", email);
res.render('application/betasignupthanks');
}
else{
models.User.create({email:email}).save(function(err, user){
if (err){
console.error(500);
return res.send(500);
}
res.render('application/betasignupthanks');
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment