Skip to content

Instantly share code, notes, and snippets.

View andrewjmead's full-sized avatar

Andrew Mead andrewjmead

View GitHub Profile
@andrewjmead
andrewjmead / app.js
Created December 6, 2015 16:36
Password Manage With Master Password Error Message
console.log('starting password manager');
var crypto = require('crypto-js')
var storage = require('node-persist');
storage.initSync();
var argv = require('yargs')
.command('create', 'Create a new account', function (yargs) {
yargs.options({
name: {
// POST /todos
app.post('/todos', middleware.requireAuthentication, function(req, res) {
var body = _.pick(req.body, 'description', 'completed');
db.sequelize.transaction(function(t) {
var t;
return db.todo.create(body).then(function(todo) {
t = todo;
return req.user.addTodo(todo)
@andrewjmead
andrewjmead / accounts.js
Created January 17, 2016 21:03
Basic Prototype Example With Accounts
function Account(balance) {
this.balance = balance || 0;
}
Account.prototype.deposit = function(amount) {
this.balance += amount;
};
Account.prototype.withdraw = function(amount) {
this.balance -= amount;
@andrewjmead
andrewjmead / todo.js
Last active April 3, 2025 07:19
Sequelize model validation
module.exports = function (sequelize, DataTypes) {
return sequelize.define('todo', {
description: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1, 250],
isString: function (value) {
if (typeof value !== 'string') {
throw new Error('Description must be a string');
@andrewjmead
andrewjmead / install
Created January 19, 2016 13:42
Installing Node.js v5.x on Ubuntu
sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
sudo apt-get install -y nodejs
@andrewjmead
andrewjmead / scrape-reddit.js
Created January 21, 2016 12:54
Request & Cheerio Scraping Example
var request = require('request');
var cheerio = require('cheerio');
request('https://www.reddit.com/', function (error, response, body) {
$ = cheerio.load(body);
$('a.title').each(function (i, elem) {
console.log('');
console.log('** Link ** ')
console.log($(this).text());
@andrewjmead
andrewjmead / app.js
Last active January 25, 2016 00:08
Adding masterPassword to get command
command('get', 'Get an existing account', function (yargs) {
yargs.options({
name: {
demand: true,
alias: 'n',
description: 'Account name (eg: Twitter, Facebook)',
type: 'string'
},
masterPassword: {
demand: true,
@andrewjmead
andrewjmead / api.jsx
Last active January 26, 2016 13:01
Marcus Post User Example
return fetch(url, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Auth': localStorage.getItem('token')
},
body: JSON.stringify({
email: userEmail,
password: userPassword,
@andrewjmead
andrewjmead / api.jsx
Last active January 26, 2016 13:54
Marcus - Updated to include headers
// Only part of file
return fetch(url, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Auth': localStorage.getItem('token')
},
body: JSON.stringify({
email: userEmail,
@andrewjmead
andrewjmead / server.js
Last active January 27, 2016 01:04
Example body-parse error handling
app.use(function(err, req, res, next) {
if (err instanceof SyntaxError) { // If invalid JSON
res.status(500).json({error: "INVALID_JSON"});
} else if (err) {
res.status(500).json({error: "SYSTEM_ERROR"});
}
});