###Prerequesites
- Install node.js from http://nodejs.org
- Make sure you install git from http://git-scm.com
Install flightplan globally
npm install -g flightplan
Install flightplan in your project folder
###Prerequesites
Install flightplan globally
npm install -g flightplan
Install flightplan in your project folder
Deploying a node app with Forever is great...until your server restarts unexpectedly. Then your app stops running and you have to re-deploy.
To get around this, we're going to run our node app as an Upstart service. Upstart services are great, because, once started, the system auto-restarts them if they fail, or if the server restarts.
###Step 1: Create a service for your node app
ssh root@youripaddress
service node-app start
##Setup your server (this would ideally be done with automated provisioning)
npm install -g forever
##Install flightplan
npm install -g flightplan
npm install flightplan --save-dev
(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
Here's all you have to do to add clustering to your node.js application.
cluster.js
, and run cluster.js
instead of server.js
(or /bin/www, or whatever it's called for your project)server.js
filevar cluster = require('cluster');
if (cluster.isMaster) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
##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
##The Good, The Bad, & The Ugly Ways of handling Async Operations With Javascript## #####Callbacks < Promises < Generators#####
###Doing Express.js Async With Generators###
Using Co library, run generators as responses by wrapping Co and adding an extra function to handle errors
//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) => {
##what are generators##
yield
a value, the function pauses until .next(modifiedYieldValue)
is calledvar myGen = function*() {
var one = yield 1;
var two = yield 2;
var three = yield 3;
console.log(one, two, three);
upstream project { | |
server 22.22.22.2:3000; | |
server 22.22.22.3:3000; | |
server 22.22.22.5:3000; | |
} | |
server { | |
listen 80; | |
location / { |