NodeJs Flashcards
CommonJS modules ( 3 characteristics)
Uses require and module.exports
Synchronous loading
Default in nodeJs
ECMAScript Modules ( 3 characteristics)
Uses import and exports , standard JS
Asynchronous loading
Works in nodejs and browsers
How to use ECMAScript modules
package.json = { “type”: “module” }
Code to create a http server
import http from ‘http’
http.createServer( (req, res) => {})
server.listen( port, err => {})
res.writeHead( code, { “header”:”content”})
res.write(data);
res.end()
fs (File System)
Handles file operations such as reading, writing and deleting files.
writeFileSync(name, text)
readFileSync( path,encoding)
path
Helps with working with file and directory paths
path.join(_dirname, filename)
https
Similar to http but handles htpps secure requests.
https.get( url, (res) => res.on())
os
Provides OS related utilities
os.platform()
os.totalmem()
os.freemem()
crypto
Provide cryptographic functions as hasing and encryption.
const hash = crypto.createHash(‘sha256’).update(‘password123’).digest(‘hex’);
events
Implements an event-driven architecture using EventEmitter.
new EventEmitter();
emitter.on(‘event’, callback)
emitter.emit(‘event’)
util
Provides utilities such as promistificattion of callback-based functions.
child_process
Execute shell commands and external programs.
exec(‘ls’, (error,stdout, stderr) => {})
stream
Handles streaming data, large files.
const readStream = fs.createReadStream(‘example.txt’, ‘utf-8’);
readStream.on(‘data’, chunk => console.log(chunk));
3 Javascript Execution data
JS is synchronous
Js is blocking
Js is single-threated
What is NodeJs Event Loop?
C program that coordinates the execution of code in Node.Js uses V8 engine and libuv library for sync and sync ops.
How does V8 and libuv work?
V8 has the memory memory head and the call stack for sync ops, libuv works only when we have async functions
What is libuv?
Libuv is a cross-platform open-source library written in C. In the Node.js runtime, its role is to provide support for handling asynchronous operations.
Does Node wait for the call stack to be empty before running the callback function, or does it interrupt the normal flow of execution to run the callback function?
Node.js does NOT interrupt the normal flow of execution to run an asynchronous callback, it waits for the call stack to be empty.
Is valid to use await at js top level file?
await at the top level is only valid in ES modules.
What are the phases of the Event Loop?
- Timers
- I/O Poll
- Check Queue
- Close Queue
What are microstasks in the Event Loop?
This tasks are process.NexThick or promises.resolve that get executed right away, without waiting for the next phase.
Does Node wait for the call stack to be empty before running the callback function, or does it interrupt the normal flow of execution to run the callback function?
Node.js does NOT interrupt the normal flow of execution to run an asynchronous callback, it waits for the call stack to be empty
If two async tasks such as setTimeout and readFile complete at the same time, how does Node decide which callback function to run first on the call stack? Does one get priority over the other?
Timer callbacks are executed before I/O callbacks, even if both are ready at the exact same time.
What are I/O ops?
Input Output ops.
Http Requests, Reading from a File, sockets