Skip to content

Instantly share code, notes, and snippets.

@learncodeacademy
learncodeacademy / gist:5f84705f2229f14d758d
Last active February 24, 2021 14:48
Getting Started with Vagrant, SSH & Linux Server Administration
@learncodeacademy
learncodeacademy / gist:5850f394342a5bfdbfa4
Last active January 7, 2024 11:58
SSH Basics - Getting started with Linux Server Administration

###SSH into a remote machine###

ssh user@mydomain.com
#or by ip address
ssh user@192.168.1.1

exit: exit ###Install Something###

#If it's a new server, update apt-get first thing
@learncodeacademy
learncodeacademy / gist:ebba574fc3f438c851ae
Created July 24, 2014 14:47
Nginx Node Frontend / Load Balancer / Static Assets Caching
upstream project {
server 22.22.22.2:3000;
server 22.22.22.3:3000;
server 22.22.22.5:3000;
}
server {
listen 80;
location / {
@learncodeacademy
learncodeacademy / generators.md
Last active January 7, 2024 11:58
What are Javascript Generators?

##what are generators##

  • They're pausable functions, pausable iterable functions, to be more precise
  • They're defined with the *
  • every time you yield a value, the function pauses until .next(modifiedYieldValue) is called
var myGen = function*() {
  var one = yield 1;
  var two = yield 2;
  var three = yield 3;
 console.log(one, two, three);
@learncodeacademy
learncodeacademy / async.md
Last active August 29, 2015 14:05
Async Generator Tests

###Doing Express.js Async With Generators###

Using Co library, run generators as responses by wrapping Co and adding an extra function to handle errors

  • Because Co always runs the last argument as a callback...so it would always runs next() by default
  • This function only runs next if there's an error
//assign this to the Co module somewhere in application bootstrapping, 
//so you can do var ce = require('co').coExpress in each router file
co.coExpress = (generator) => {
  return (...args) => {

##The Good, The Bad, & The Ugly Ways of handling Async Operations With Javascript## #####Callbacks < Promises < Generators#####


###An Example: 5 in-sequence Async Operations### (also see parallel-sequence example: https://gist.github.com/willrstern/af3a3308fc5864cf48f8)
###The Ugly Way: Callbacks### After each function takes place, handle any errors & do the next thing - It's easy to walk through the code and understand what's going on...but it's ugly as sin

##Example Task: Make 2 parallel (simultaneous) async calls, then one async call when they've resolved##

##The Good Way: Promises##

//with bluebird library
var parallelCalls = [
  getUrl('/api/profile'),
  getUrl('/api/accounts')
];
//spread is like .then(), except it apply()s the array of responses as individual arguments
@learncodeacademy
learncodeacademy / cluster.md
Created October 9, 2014 18:11
Node Cluster - Enhance your node app by using all the cores of your processor.

Here's all you have to do to add clustering to your node.js application.

  • save this code as cluster.js, and run cluster.js instead of server.js (or /bin/www, or whatever it's called for your project)
  • the only line you'll need to change is the last line - it needs to point to the location of your server.js file
var cluster = require('cluster');

if (cluster.isMaster) {
  // Count the machine's CPUs
 var cpuCount = require('os').cpus().length;
@learncodeacademy
learncodeacademy / deployUser.md
Last active October 8, 2022 18:56
Adding a deploy user in Linux

(wherever it says url.com, use your server's domain or IP)

Login to new server as root, then add a deploy user

sudo useradd --create-home -s /bin/bash deploy
sudo adduser deploy sudo
sudo passwd deploy

And Update the new password

@learncodeacademy
learncodeacademy / flightplan-deploy.md
Last active January 7, 2024 11:58
Deploy Node.js Apps with Flightplan

##Setup your server (this would ideally be done with automated provisioning)

  • add a deploy user with password-less ssh see this gist
  • install forever npm install -g forever

##Install flightplan

  • npm install -g flightplan
  • in your project folder npm install flightplan --save-dev
  • create a flightplan.js file