Core modules:
- http→ Launch a server and recieve requests
- https
- fs
- path
- os
- buffer (no need to import)
createServer
http.createServer, takes a callback function which gets two params request and response and returns a server. Server is an object with method listen.
Node lifecycle & Event loop
Entire node process uses only one loop.
process.exit ends the loop (process is available as global object in your node script)

Requests and Responses
- How will node internally call req on data? If you do ctrl + c, cpp code will determine something has changed and clear the stack for end event methods before terminating, anyinfinite_loop here will keep the process waiting.
Request = {
url: string;
method: string | 'GET' | 'POST';
headers: Object,
on: (event, cb) => {
if (event === 'data') {
// what data has come from stream internally pass it to cb
cb(dataServerGot)
}
if (event === )
}
}
Response = {
statusCode: Number,
setHeader: (key, val) => {
},
write: (content: string) => {
// append some content here
},
end: () => {
// will this call process exit?
}
}- How to parse the buffer data?
- Ans. Buffer.concat(contentChunkArr).toString()
fileSystem module
- our request is a stream.
- buffer is chunk of data that come together const fs = { writeFileSync: (filename, contentToWrite) ⇒ {
}, writeFile(filename, message, cbError) { // will write file really fail, guess can fail if the size is too large or message is empy or something // offloads the job to operating system } }
- task Q. How do write file sync execute asynchronously?
- task Ans. It blocks the main thread, synchronous function should only be used when debugging.
Single thread, Event loop and Blocking code
- Q. How are multiple request processed if it issingle_threaded
#worker_pool → does all heavy lifting. (handles multiple threads)
Event loop is a loop which is keeps the process running and handles all callbacks.
- First checks timers (setTimeout etc),
- pending callbacks (execute)
- Poll (retrieve new IO events, execute their callbacks) after can just to timers step 1
- Check (execute setImmediate() callback)
- close callbacks (all close event callbacks)
- process.exit (if no remaining event handlers.) http.createServer is example of one event that keeps running and thus ref is never 0
Modules
module.exports = anyModuleName;
module.exports = {
some: 'propHere',
something: () => `I'm a method`
}
module.exports.someText = 'someText'
const routes = require('./routes')