Skip to content

dig deep into node.js

overview

  • client need to request api from server(interacted with server's network and file system, bypass cpp) AKA node

Nodejs, JS, compute

Node Feature

  • save data and functionality
  • use that data by running functionality on it
  • Has a ton of built in labels that trigger Node features

execute js

  • thread of execution
  • execute(RUN) code line by line
  • save the label and value in memory
  • execute code and save the result in memory
  • wdym execute?
    • execution context
    • save code and data
    • run code on data

Execution Node

  • use built in labels to trigger CPP features then interact with computer's original features

Using Node APIs

Node label

const server = http.createServer();
server.listen(3000);
  • the server includes a object, then you could assign port by listen func

calling function

  • execute the function
  • inserting input

2-auto-created-inserted data(arguments) functions

  • object of functions(end, write, etc)
  • auto-run functions
  • object of url, method, content-type, etc

node with HTTP

Do on Incoming Request

when call http.createServer() in js, it will return an object with methods of http(to help us customize the config of http in NodeJS), in NodeJS, it will trigger the network part in computer, like open a socket to let info in and out

Error handling

  • use on to auto-run func
  • request, do on inserting
  • clientError, do on Error

auto insert func

  • err(default to null)
  • actual data

call stack

  • global scope
  • a stack to store other processes

Stream

  • chunk of data
  • In JS, when it call node function, it will return a object with methods of xx
  • In async, the callback function will be stored in task queue
  • In event loop, if call stack is empty, it will pop the callback function from task queue and push it to call stack