Node.js Notes

from: https://thenewboston.com/tutorials/All/10bec649-967d-4b0d-bc0d-e3c9eacda40c

'==' vs '==='
console.log(19 == '19') //true
console.log(19 === '19') //false
javascript pointer shit
var Bucky = {
       favFood: "bacon",
       favMovie: "Chappie"
};

var Person = Bucky;
Person.favFood = "salad";
console.log(Bucky.favFood); //the print result would be "salad"
nodejs “this”
var Bucky = {
       printFirstName: function() {
            console.log("My Name is Bucky");
            console.log(this === Bucky);
       }
};
Bucky.printFirstName(); //prints "True"

//The default calling context is global
function doSomethingWorthless(){
      console.log("\nI am a worthless piece of shit");
      console.log(this === global);
}
doSomethingWorthless(); //prints "I am ... shit" and "true"
nodejs Prototyping
function User(){
     this.name = "";
     this.life = 100;
     this.giveLife = function giveLife(targetPlayer) {
          targetPlayer.life +=1;
          console.log(this.name + "give 1 life to " + targetPlayer.name);
     }
}

var Bucky = new User();
var Wendy = new User();
Bucky.name = "Bucky";
Wendy.name = "Wendy";

Bucky.giveLife(Wendy); //now Wendy 101, Bucky 100 lives.

//you can add functions to all objects
User.prototype.uppercut = function uppercut (targetPlayer){
     targetPlayer.life -=3;
};

Wendy.uppercut(Bucky); //now Wendy 101, Bucky 97

//You can add properties to all objects
User.prototype.magic = 60;//now both Wendy and Bucky have 60 magics.
Nodejs Modules
//filename: movie.js
module.exports ={
     printAvatar: function(){
          console.log("Avatar");
     },
     printChappie: function(){
          console.log("Chappie");
     },
     favMovie: "The Matrix"
};

... is the same thing as

//filename: movie.js
//module.exports = {};

function printAvatar(){
     console.log("Avatar");
}

function printChappie(){
     console.log("Chappie");
}

var favMovie = "The Matrix";

module.exports.PrintAvatar = printAvatar;
module.exports.printChappie = printChappie;
module.exports.favMovie = favMovie;

in the importing javascript file, you type:

var movies = require('./movies');
movies.printAvatar();
movies.printChappie();
Object Factory
//movies.js
module.exports = function () {
     return{
          favMovie: ""
     }
};

in emily.js:

var movies = require('./movies');

var emilysMovies = movies();
emilysMovies.favMovie = "The Notebook";
console.log("blablabla" + emilysMovies.favMovie);

in bucky.js

var movies = require('./movies');

var buckysMovies = movies();
console.log("blablabla" + buckysMovies.favMovie);

in app.js

require('./emily');
require('./bucky');

The terminal will print:

blablabla The Notebook
blablabla
Creating a Basic Server
var http = require('http');

function onRequest(request, response){
     console.log("A user made a request" + request.url);
     response.writeHead(200, {"Contect-Type": "text/plain"}; //string basically
     response.write("Here is some data");
     response.end();
}

http.createServer(onRequest).listen(8888);
console.log("Server is now running...");
Connect
var connect = require('connect');
var http = require('http');

var app = connect();

function doFirst(request, response, next){
     console.log("Bacon");
     next();
}

function doSecond(request, response, next){
     console.log("Tuna");
     next();
}

app.use(doFirst);
app.use(doSecond);

http.createServer(app).listen(8888);
console.log("Server is running.");

Optional paging:

var connect = require('connect');
var http = require('http');

var app = connect();

function profile(request, response){
     console.log('User requested profile');
}

function forum(request, response){
     console.log('User requested forum');
}

app.use('/profile', profile);
app.use('/forum', forum);

http.createServer(app).listen(8888);
console.log("Server is running.");