Log in

Sign in with your email address and password:

No account? Sign up here.
Forgot your password? Reset it here.
Didn't receive account confirmation email? Request a new one.


NodeJs Cheat Sheet

NodeJs is a very powerful web development language, built upon the V8 engine, NodeJs is renowned for it’s speed and offers the added benefit of giving developers one language to code on both front – and backends with!

NodeJs Cheat Sheet

Node & V8 background

V8 node.js engine is written in C++
ECMAScript – standard JS is based on: the way JS should work

Modules, exports, require

Make otherwise unavailable objects available to importing modules
modules.export = {thing}
require({moduleName.js |moduleName/index.js}

First-class function:

Can use functions like strings, numbers, etc – pass them around, use them in arrays, etc

JS Objects:

Nothing but a collection of name/value pairs
If value of a member is a function, then it is a method.

Inheritence (Prototypal)

Pass by Value, Pass by Reference

Objects are passed by reference, primitives by value (pretty standard really)

Immediately Invoked Function Expressions

Modules & Require

Build modules by putting .js files into folder {folder}
require {folder} will require {folder}/index.js

Revealing Module Pattern: Expose only properties & methods you want via returned object
(encapsulation)

Require Core Modules

https://nodejs.org/api/index.html
These can be imported …

Events

Two kinds of events:

  • System events – c++ side of node.js core. From libuv for example
  • Custom events: JavaScript core – events I create for myself – Event Emitter

Node Event Emitter

Class usage in ES6

Callbacks

Code you give to other code to be run at some point in time (eg. when event triggers)
function (callback) {
callback();
}

Libuv, Event-loop, non-blocking asynchronous code

libuv talks directly to the OS
has a queue of events
Non-blocking event-driven IO architecture

Pipes

Connecting two streams by writing to one stream what is being read from another …

Port

Once a computer receives a packet, how it knows what program to send it to

Web Server

Routing

NPM Packages

Versions: semantic versioning

{major}.{minor}.{patch}
* patch increment: Some bugs were fixed. Updating will not break your code
* minor increment: Added new features. Updating will not break your code
* major increment: Big changes. Your code will very possibly break

Create package.json (use npm init or manually create it)

Installing Packages

In package.json is created:

‘^’ means only update on minor (or lower) release changes
‘~’ means only update on patch RCs

Now, if node_modules are ever deleted, simply run ‘npm install’ and it will
download all dependencies into node_modules again (kinda like composer creates ‘vendor’ folder in PHP)

use —save-dev to put into devDependencies

Web Server Enhanced – Express

Takes care of a lot of the HTTP stuff for us, smartly!
https://expressjs.com/en/starter/hello-world.html

NodeJs and MySql

Access MySQL database using the mysql package:

NodeJs and NoSQL

NoSQL – allows us to store data objects as is (ie not in tables). Lets us modify the structure of objects very easily.
Use package ‘mongoose’

MEAN Stack

MongoDb, Express, AngularJs, NodeJs

AngularJs

Helps update & manipulate the DOM.

Templating engine:

app.js (also include angular from CDN):


Submit a Comment