-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnodejs.txt
More file actions
183 lines (125 loc) · 5.76 KB
/
nodejs.txt
File metadata and controls
183 lines (125 loc) · 5.76 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
* Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser.
* NodeJs is a C++ program that includes Google's V8 engine.
* Express.js, or simply Express, is a web application framework for Node.js
Commannds:
----------------------------------
* npm init (OR) npm init --yes (OR) npm init -y
* npm list = to see all the dependency packages
* npm install underscore (OR) npm i underscore
* npm i -g lodash = It install "lodash" package globally
* npm root -g = it gives the path that where all the global packages are instaling
* npm i -g npm@5.0.1 = This is how we can install a package with particular version
* npm uninstall -g npm (OR) npm un -g npm (OR) npm remove -g npm (OR) npm rm -g npm = It removes globally
* npm uninstall underscore (OR) npm un underscore (OR) npm remove underscore (OR) npm rm underscore
* npm i mocha --save-dev (OR) npm i -D mocha
* npm list (OR) npm ls = To get all the modules with version number that are installed.
* npm list --depth=0 (OR) npm ls --depth=0 (OR) npm ls --depth 0 = To get only the modules with version number that are installed by us.
* npm view mongoose = To view package.json file a particular module
* npm view mongoose dependencies
* npm view mongoose versions
* npm outdated
* npm update mongoose@
* npm i -g npm-check-updates
* npm config get init-author-name = It gives the default author name
* npm config set init-author-name "Manoj" = It sets the default author name to "Manoj"
* npm config delete init-author-name = It deletes the previously set author name to default
* npm cache clean --force
-----------------------------------
Semantic versioning (SemVer) example = 5.0.1
5 = Major
0 = Minor
1 = Patch release
^5.0.1 = 5.X
~5.0.1 = 5.0.X
* Every node module contains package.json file
Few ExpressJS middlewares
--------------------------------------
helmet = Secures our app by setting variour HTTP headers
app.use(helmet());
morgan = logs all the HTTP requests -> It impacts the request process pipeline
app.use(morgan('tiny'));
----------------------------------------
* Function expressions
* Module & Require()
* Read, Write & delete files
* Create, remove directories
* Protocols: A set of communication rules , that two sides agree to use when communicating.
* HTTP is used for websites whereas FTP is used for transfer files.
* A programme running on a computer can listen for requests sent to a particular port.
* Client & Server topics
* Streams & Buffers
* Basic routing using NodeJS
* NPM concept
* 'Ryan Dahl' embeded Google's V8 engine into C++ and named it as 'Node'
* Node is a run time javascript environment. It contains javascript engine that executes javascript code.
* Every file in Node is considered as Module.
* Variables and functions that are declared in a file are scoped to that module.
* If we want to use a variable or a function which is defined in a module from outside of that module we need to export and then import that module.
* Every Node application has at least one file/module which is called "Main Module".
* The type of the HTTP request determines the kind of the operation.
*
********************************************************************************************
Creating server with pure NodeJS and serving files with headers:
--------------------------------------------------------------------
const http = require('http');
const path = require('path');
const fs = require('fs');
const server = http.createServer((req, res) => {
if (req.url === '/') {
fs.readFile(
path.join(__dirname, 'public', 'index.html'),
(err, content) => {
if (err) throw err;
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content);
}
);
}
if (req.url === '/api/users') {
const users = [
{ name: 'Bob Smith', age: 40 },
{ name: 'John Doe', age: 30 }
];
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(users));
}
********************************************************************************************
Payment Gateway:
--------------------------------------------------------------------------------------------
* https://www.youtube.com/watch?v=mI_-1tbIXQI
* https://www.youtube.com/watch?v=PsvxhUAw26o
* https://www.youtube.com/watch?v=QT3_zT97_1g
NodeJS core modules:
--------------------------------------------------------------------
HTTP, path, fs, os, events, buffer, crypto
-> FS functions
-> fs.watch(filePath, () => {});
-> fs.readFile(filePath, (err, data) => {});
-> fs.readFileSync(filePath);
* https://thebittheories.com/nodejs-interview-questions-935498510d46
* http://www.tutorialspoint.com/nodejs/nodejs_interview_questions.htm
* https://www.youtube.com/watch?v=-PuMp5tQ8Jw&list=PL4cUxeGkcC9jdm7QX143aMLAqyM-jTZ2x&index=15
****************************************** IMP points ************************************************
* JWT contains 3 parts
-> Header - Algorithm token type
-> Payload - It contains the data that we store/sent
-> Signature
* 'middleware' is just a function that has access to the request & response cycle.
****************************************** Status codes ************************************************
* 401 = Un-Authorized
* 400 = Bad request
* 500 = Internal server error
* 404 = Not found
* 408 = Request timeout
*
************************************** Askable Intervirew Q's ******************************************
* What's Node JS?
* Explain 'Status codes'?
* Write sample express server connection with MongoDB connetion?
* What is a 'middleware'?
* How do you protect the routes?
*
************************************** Mongoose functions ******************************************
* findById(id);
* findOne({ email });
* findById(id).sort({date: -1});