Skip to content

Instantly share code, notes, and snippets.

View andrewjmead's full-sized avatar

Andrew Mead andrewjmead

View GitHub Profile
@andrewjmead
andrewjmead / demo.js
Created May 28, 2016 23:03
Jerome Argument Names
function add (a, b) {
return a + b
}
var result = add(1, 2);
console.log(result);
@andrewjmead
andrewjmead / app.js
Created May 18, 2016 13:07
Sarvesh promise
User.find({}).then(function (users) {
// Do something with the users data
}, function (err) {
// Do something with the error
});
@andrewjmead
andrewjmead / app.js
Created May 17, 2016 13:23
Yargs 4.x.x.
var argv = require('yargs')
.command('Hello', 'Greets the User', function(yargs){
return yargs.options({
name: {
demand: true
}
});
})
.argv;
var command = argv._[0];
@andrewjmead
andrewjmead / script.js
Created May 17, 2016 00:45
Example of custom this keyword
var obj = {
age: 99,
someFunc: function () {
console.log('age', this.age);
}
}
// Uses the default this definition which would be the obj object
obj.someFunc();
@andrewjmead
andrewjmead / server.js
Created May 16, 2016 12:57
Redirect To HTTP Always
// ...
app.use(function (req, res, next){
if (req.headers['x-forwarded-proto'] === 'https') {
res.redirect('http://' + req.hostname + req.url);
} else {
next();
}
});
@andrewjmead
andrewjmead / app.js
Created May 1, 2016 22:27
Jerome Try/Catch
/**
* In the following example, the code in the try block will trigger the catch block
* without explicitly calling "throw new Error('error message')"
*
* When you try to reference a variable that does not exist, Node itself will throw
* the error.
*/
try {
console.log(varNotReal);
@andrewjmead
andrewjmead / package.json
Last active April 28, 2016 13:32
Rafael Package.json File
{
"name": "t",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack ./public/app.js ./public/bundle.js"
},
"author": "",
@andrewjmead
andrewjmead / cart-reducer.js
Last active April 21, 2016 11:38
Marcus Cart Reducer
import { ADD_TO_CART } from '../actions/index';
export default function(state = [], action) {
switch(action.type) {
case ADD_TO_CART:
return [
...state,
action.payload
];
default:
@andrewjmead
andrewjmead / usersController.js
Created April 15, 2016 11:28
Seth GET /user
app.get('/user', function(req, res) {
db.user.findAll().then(function(user) {
res.json(user.toPublicJSON());
}),
function(e) {
res.status(500).send();
}
});
@andrewjmead
andrewjmead / app.js
Created April 10, 2016 14:05
Auchomag - Variables
// EXAMPLE ONE
var foo = 'Andrew';
console.log(foo); // 'Andrew'
function foo () {
return 'From foo function'
}
console.log(foo()); // 'From foo function'