-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.txt
257 lines (202 loc) · 8.95 KB
/
node.txt
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
server-side JavaScript runtime environment built on Chrome's V8 JavaScript engine
It allows developers to run JavaScript code outside the web browser, typically on the server side
Pros
------
Non-Blocking I/O
---------
Node.js is based on an event-driven, non-blocking I/O model, which allows it to handle multiple concurrent connections efficiently.
This makes it well-suited for building real-time applications, such as chat applications, online gaming platforms, and streaming services
Performance
-----------
compiles JavaScript code into native machine code for execution. This results in high-performance applications with fast response times
NPM Ecosystem and Community and Support
Cons
-------
Single-Threaded
--------------
Node.js is single-threaded, meaning it uses a single event loop to handle all incoming requests
Callback Hell
------------
multiple nested callback functions are used to handle asynchronous operations
asyncFunction1(function(result1) {
asyncFunction2(result1, function(result2) {
asyncFunction3(result2, function(result3) {
asyncFunction4(result3, function(result4) {
// Do something with result4
});
});
});
});
overcome the single-threaded
-----------
Use Worker Threads
------------
allows you to create and run multiple threads of execution alongside the main event loop
callback
-----------
A callback function in JavaScript is a function that is passed as an argument to
another function and is executed after some asynchronous operation.
advantages of using promises instead of callbacks
-------------
built-in error handling and Improved readability
uses of node js
---------
Real-time chats, Internet of Things, Streaming applications, Microservices architecture
modules
----------
modules are reusable units of code. They allow you to organize your code into smaller, manageable pieces, making it easier to
maintain, reuse, and share across different parts of your application.
core modules => require('path')
File modules => require('./app.js')
Why is Node.js preferred for backend?
--------------
Node.js is very fast
Node Package Manager
real-time web applications, as Node.js never waits for an API to return data
Better synchronization of code between server and client due to same code base
Event Loop
------------
Event loops handle asynchronous callbacks in Node.js. It is the foundation of the non-blocking input/output in Node.js
EventEmitter
---------------
Node.js uses events module to create and handle custom events
The EventEmitter class can be used to create and handle custom events module
types of API functions in Node.js
------------
Asynchronous non-blocking functions and Synchronous blocking functions
package.json file?
-------------
The package.json file is the heart of a Node.js system. This file holds the metadata for a particular project.
Express JS
--------
is a minimalist and flexible web application framework for Node.js
Is creating server-side logic and handling HTTP requests
Routing
--------
It allows you to define routes to handle different HTTP requests
GET,POST,PUT,DELETE,PATCH
Error Handling and RESTful API Development
streams in Node.js?
------------
Streams are objects that enable you to read data or write data continuously.
Types => Readable (used for), Writable, Duplex (both read and write), Transform
REPL ( Read Eval Print Loop) in Node.js?
-----------
It’s similar to a Windows console or Unix/Linux shell in which a command is entered
Read - reads users input
Eval - evaluates the data structure
Print - print the result
Loop - Loops above the comments
buffer class in Node.js
-------------
Buffer class stores raw data similar to an array of integers but corresponds to a raw memory allocation outside the V8 hea
piping in Node.js?
-------------
Piping is a mechanism used to connect the output of one stream to another stream
middleware
------------
Middleware is a function that receives the request and response objects
Used for, Update or modify the request and the response objects and Finish the request-response cycle
types of HTTP requests?
GET,POST,PUT,DELETE,PATCH
Node.js timing features
---------------
setTimeout, clearTimeout
setInterval, clearInterval
setImmediate, clearImmediate
Node.js better than other frameworks?
----------------
Node.js is a server-side JavaScript runtime environment built on top of the V8 JavaScript engine, the same engine that powers Google Chrome
Node.js very fast and efficient, as well as highly scalable.
fork in node JS?
-----------
The Fork method in Node.js creates a new child process that runs a separate Node.js instance
JavaScript Vs Node.js
-------------
Feature | JavaScript | Node Js
Definition | JavaScript is a programming language | Node.js is a runtime environment
Execution | Runs in web browsers | Runs on servers
Platform | web | server
Event Loop | Not applicable (applicable by web browser) | applicable
asynchronous vs synchronous
------------------
Feature | asynchronous | synchronous
Execution | without waiting for the task to complete | waiting for each task to complete before moving on to the next one.
Task Handling | Handles tasks that involve I/O operations | Handles tasks that are CPU-bound
Control Flow | Uses callbacks, Promises, or async/await | Follows a linear execution flow
Error Handling | Requires careful error handling | Errors are easier to handle
primary reason for using the event-based model in Node.js?
-----------------
The main reason to use the event-based model in Node.js is performance.
Node.js can handle a large number of connections without using a lot of resources.
Node run on Windows?
---------------
Yes, Node.js runs on Windows. Node.js is a cross-platform runtime environment
access DOM in Node?
--------------
No, you cannot access the DOM in Node.js. DOM is a browser-specific API,
Since Node.js does not run in a browser
stub in Node.js
--------------
stub is a function that serves as a placeholder for a more complex function. Stubs are typically used in unit testing
LTS releases of Node.js?
-------------
LTS stands for Long-term support. LTS releases of Node.js are versions that are supported for an extended period, usually for 30 months from the time of release
ESLint ?
----------
ESLint is a popular open-source tool that is used to analyze and flag errors
Node.js handle the child threads?
---------------
Node.js handles child threads by creating separate instances of the Node.js runtime environment that can be used to execute code in parallel with the main process.
microservices
----------
microservices refer to an architectural approach for building applications as a collection of small, independent services
Each service focuses on a specific services
MVC (Model-View-Controller)
----------------
is a popular architectural pattern used in Node.js development for structuring web applications
Model => This component represents the data and business logic of your application.
View => This component deals with the user interface (UI) representation of your application
Controller => This component acts as the intermediary between the model and the view.
Code Reusability and Testability
functions vs middleware
---------------
features | functions | middleware
Purpose | Reusable block of code performing a specific task | Intercepts requests and responses in the application flow
Arguments | any arguments | req,res,next
Return Value | any value | modified req,res
Scope | called from anywhere | Executed in a chain during request processing
usages | calculations, logic execution | Authentication, authorization, logging, error handling
Reusability | Highly reusable across different parts | Designed for specific middleware tasks
body parser
----------
is a middleware that simplifies handling incoming HTTP request bodies
Simplified Data Access, Format Handling, improved Developer Experience
Cron
--------
Cron, in the context of Node.js, refers to scheduling tasks to run at specific intervals.
It's not a built-in functionality of Node.js
every minutes => * * * * * (minutes hour day month week)
every 2 minutes => */2 * * * *
CORS (Cross-Origin Resource Sharing)
----------------
is a mechanism that restricts web browsers from making requests to a different domain
node red?
----------
Node red is a visual programming tool for Node.js that is used to wire
hardware devices and online services as part of IoT applications.
Redis
------
Redis is an open-source, in-memory data structure store that can be used as a database, cache, and message broker.
it is commonly referred to as a data structure server
Session Storage, Queueing, Caching, Pub/Sub Messaging
Query Parameters (req.query)
----------
Query parameters are key-value pairs appended to the end of a URL after a question mark
Route Parameters (req.params)
-----------
Route parameters are dynamic parts of the URL path specified using placeholders in the route definition
Request Body (req.body)
--------------
The request body contains data sent by the client as part of an HTTP request
data form are JSON and form data