-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNodeJS Basics.txt
More file actions
185 lines (126 loc) · 4.79 KB
/
NodeJS Basics.txt
File metadata and controls
185 lines (126 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
Node + React = Front End (Web/App)
Node + Express = Back End (Web Service)
Node + React Native = Mobile App
Package Management
------------------------------
{root_of_project_folder}$ npm init -y // answers yes with all defaults and
generates baseline package.json file
npm config set prefix <user-home-dir> // this ensures that modules installed with -g flag will not require sudo to install as they will be installed in the dir you desire
npm install // install all modules listed in package.json
npm i <package> // installs a specific package locally to the project
npm i <package> -g // installs package globally
npm i <package> --save-dev // saves module under the "devDependecies" section
npm i -g yarn
yarn --version
yarn global bin
yarn add <package_name>
yarn global add <package_name>
Yarn add <package> --dev
npm root -g
npm list -g --depth=0 // get list of all globally installed packages
yarn global list
**It is best to avoid global modules as much as possible. Define everything at the project level so that if I am collaborating with someone on a project they will find everything they need in package.json. Items that are installed globally do not show up in package.json therefore it can be problematic. Also defining things at the project level allows me to use specific versions of things based on my application needs. Global modules force me to use one version for multiple projects
Debug
---------
Insert "debugger" anywhere in your code (this will act as a breakpoint in the debug session)
$ node inspect app.js // inspect command spawns a debug session and sends it to the Chrome Browser
Open up Chrome > hit URL chrome://inspect
Inspect the current session that you spawned in step 1
Web Servers
------------------
[Live-Server]
yarn global add live-server
npm i -g live-server
{root_of_project_folder}$ live-server [folder to serve up]
{root_of_project_folder}$ live-server public/
[Express]
Classes
--------
class Person {
constructor(name = 'Anonymous', age = 0) {
this.name = name
this.age = age
}
getGreeting() {
return `Hello my name is ${this.name}!`
}
getDescription() {
return `Hello my name is ${this.name} and I am ${this.age} years old!`
}
}
class Student extends Person {
constructor(name, age, major) {
super(name, age) // call superclass constructor for name and age property
this.major = major
}
hasMajor() {
return this.major
}
getDescription() {
let description = super.getDescription() // call superclass getDescription method
if(this.hasMajor()) {
description = description + ` My major is ${this.major}!`
}
return description
}
}
const low = new Person("Low", 29)
console.log(low.getGreeting())
const other = new Person()
console.log(other.getGreeting())
Functions
---------
[Standard]
[Arrow]
Callback Functions
-------------------------
const geocode = (address, callback) => {
setTimeout(() => {
data = {
latitude: 0,
longitude: 0
}
callback(data) // define the data I want to return to the function
}, 2000)
}
geocode('TempleHills',(data) => { // data is the variable I want to access when I call the function geocode
console.log(data)
})
- A callback function is a function that we provide as an "argument" to another function with the expectation that the original function is going to call that function sometime in the future
- When a function is doing a synchronous operation, we can use the keyword "return" to have the data returned to the function that is being called.
- When a function is doing an asynchronous operation, the "return" keyword is not an option. I must use the "callback" method in order to access that data (when it becomes available)
Promises
--------
Async/Await
------------
Statement Format
-------------------
var square = (argument) => {
var result = x * x;
return result;
};
console.log(square(9));
Expression Format
--------------------------
var square = (x) => x * x;
// Simplifies our code and makes it easier to scan
// I also don't need to specify "return" keyword; it is already implied
var square = x => x * x;
// When I am accepting only 'one' argument then I don't have to wrap the variable in ()
var user = {
name: 'Andrew',
sayHi: () => {
console.log(`Hi`);
}
}
// How to use arrow function and reference an object literal (this.property)
var user = {
name: 'Andrew',
sayHi: () => {
console.log(`Hi ${this.name`); // this will not work with arrow functions
},
sayHiAlt () { // this is the correct way
console.log(`Hi ${this.name}`);
}
}
user.sayHiAlt()