Nodejs Ecosystem in a Nutshell - 401-advanced-javascript-aimurphy/seattle-javascript-401n13 GitHub Wiki
This is a work in progress...
A bit about Node.js
Node is a service we can use on our local machines that allows us to write javascript outside of the browser. This means we not only can we use javascript to write front end web apps but now we are able to write our servers create tests and a host of other tasks can now be programmed using this one language.
Node is fast. It is ASYNCHRONOUS and non-blocking, meaning that instead of running and completing each function before moving on to the next, when an event triggers a function the function will run a callback and that allows us to move on to the next while that call back runs in the background.
So, node is not a language, it is not a library, it is a service. We can write with it using javascript and it has a ton of libraries we can import to help us write even better code. We can use NPM to get those.
NPM is the Node Package Manager, an online repository of packages containing node modules. The modules are javascript modules written specifically for node containing libraries, frameworks, tools, etc. Once installed, these packages get stored in the node_modules file. When you create projects you'll also have a package_JSON file which stores information about that project including the dependencies (which versions of which node modules) were involved in its creation.
**Built-in Modules File System (fs) module exports functions for working with files and directories. HTTP (http) module provides functionality for running HTTP servers and making HTTP requests.
Get started with Node on Travis CI
go here and good luck.
ES6 gives us tools to write with!
Find out more about its features and get a brief introduction.
Arrow functions =>
First off, there's no more var, var is a dirty word. From now it's let. With arrow funcitons we are able to cut out a lot of extra noise, writing the same functions in fewer characters. So instead of writing:
let plusOne = function(v){
return v+1;
};
we can simply write:
let plusOne = x => x + 1;
Classes and inheritance
Classes are new to me so I will have to read more on that, but the jist of it is that now with ES6 we are lining up better with object oriented languages by sharing some of the lingo.
Default parameters ()
you have the ability to set a default value if there isn't one provided:
function love (x, y =" love", z =" cookies!") {
return x + y + z
}
console.log(love("me"));
//Or as an arrow fn
let love = (x, y =" love", z =" cookies!")=> x + y + z;
console.log(love("me"));
Destructured assignment
You can use this on objects or arrays to break down their contents into individual variables
Generators
a type of iterator
Iterators
It... erates.
Maps
This is supposed to be a cleaner way to substitue objects to store a key value pair???
Promises
These are what lets us run asynchronous calls. We are able to run functions and the promise fires off and waits to be fulfilled but once it is fired we can move to the next function. COOL!
I don't know how to write these very well yet. I'll come back with a good example.
Rest parameters
Take the rest of the arguments and cram them into a single param using the spread operator like so:
function favNum (str, ...a) {
return (str + a.length);
}
favNum('My favorite number is ', 2, "hello", true, 7);
Sets
sets are another space saver for when we are using common algorithms or reusing datasets... it helps reduce redundancies. Here are some methods you can use with it:
and here is a crappy example :)
let greeting = new Set()
greeting.add("oh").add("hai").add("dere")
console.log(greeting.size);//3
console.log(greeting.has("hello"));//false
for (let value of greeting) {
console.log(value);
}//oh hai dere
Spread operator ...
takes and array and converts into a list of parameters like so:
let plus = (a, b, c) => a + b + c;
let words = ['me ', 'love ', 'cookies!'];
console.log(plus(...words));
Template Literals ${}
This is one of my favorite things I have learned so far and I love to use it. Instead of writing out a complex string we can just inject the variables we want in the natural flow, like this (don't forget to swap the quotes for back-ticks!):
let when = {day: 'today'};
let greeting = `you look handsome ${when.day}!`;
console.log(greeting);
Test-Driven Development
As growing developers it's really important we refine our craft--write better code--and test driven development (TDD) is one way of helping with that. We want to build code that may have a lot more upfront investment but it will pay us back by requiring less maintenance over time, rather than hacking it out on the fly. That's right, you are writing out the test before you write out the application code.
Although it is tedious, boring and hard, and it involves the word TEST which is a problem all itself; this article outlines three great reasons for incorporating TDD into your routine:
It forces you to think: in order to write a test you are forced to clearly define what you want your program to do and not to do. If you are unable to do this then it gives you a chance to evaluate your understanding of the problem you are trying to solve. Makes debugging easier: you'll need to write out your tests as simple as possible to get the most benefit out of this, but although it is a lot of writing, it will definitely make it easier to track bugs since you wont have to dig through complexly nest functions Coding becomes more fun: once you get the hang of it, TDD can become addictive... addictively fun.
How to TDD: Red, Green, Refactor
First you write your test to fail. Then you write it to pass. Then you clean up that code and write it more simply and succinct.
Test themselves are going to be written in a format that follows behaviorally driven development, or BDD. Your test should describe a thing (such as a module, or a class, or a method, or a function) and expect it to do something.
Again, more succinctly:
Describe [thing].
It [should do something].
And expect[thingActualValue].to.equal.[thingExpectedValue];
'use strict';
var expect = require('framework').expect;
describe('thing', function() {
it('should do something', function() {
var thing = require('./thing.js');
expect('thing').to.equal.('thing');
//expect(actualValue).to.equal.(expectedValue);
});
});