Server side JS Flashcards
this keyword
Every function has one
this refers to the owner(class)/executor(outer funciton) of the function
global functions refer to the global object
undefined in strict mode
possible to use call to bind this to a specific object…
logMyVar.call(myObj)
issues with this
anonymous functions are executed globally… so this would refer to the global object.
Arrow functions
Do not define their own “this” but inherit from their parent. (can be used to fix the anonymous global function problem)
short version of a function function(x, y) {...} (x, y) => ...; or (x, y) => {...};
Immediately Invoked Function Expressions (IIFE) (Definition)
Java script function that runs as soon as it is defined.
Ex:
var result = (function () {
…
})()
IIFE Useful for…
In this module: var myPerson = { ssid: "12345", getSSID: function() {this.logSSIDReq(); return ssid;}, logSSIDReq: function() { console.log("SSID returned"; } };
everything is accessable…
can use IIFE to create some privacy!
var myPerson = (function(){ var ssid= "12345"; var getSSID= function... ... return {getSSID: getSSID}; })()
everything is the same except = instead of : and =(function(){})() instead of ={}
What is Node.js?
JavaScript runtime…
Allows you to run javascript on a server and build command line tools.
No DOM and NO HTML events
Node.js modules
Some built in modules
-http for making and handling HTTP reqs
- fs for file i/o
- crypto for cryptography ops
- assert for assertions (testing)
Include modules using the require statement
Ex:
var myHttp = require(‘http’)
NPM
Node Package Manager
Allows to install modules that are not your own and not included in Node.js
Installed modules are included using require()
How to share Node software
Define an NPM project:
npm init -also creates a package.json To install a new module npm install --save [moduleName] -installs in current dir and saves dependency in package.json
npm install looks up all dependencies in package.json and installs them
Express module
const express = require('express'); const app = express();
app.get(‘/’, (req, res) => {
res.status(200).send(‘Hello World!’);
});
ETC
MongoDB
Find documents:
db.collection(“collectionName”).find({}).toArray(function(err, users) {
res.status(200).json(users);
});
also (instead of find): insertOne() findOne() findOneAndUpdate(); deleteMany()