Un-tit-led
Back-End Development and APIs Certification (freeCodeCamp)
📓

Notebook

Introduction to Node.js

    Working with NodeJS and even driven architecture
  • Client-side JavaScript(JS) limitations: restricted access to local files, not for handling complex application logic, security concerns.
    The reason is because it was designed to run exclusively on web browsers.
  • Web browser provide the environment that is needed to run JS code, including JS engine and provide access to DOM so you can access HTML elements in your code.
  • We can run JS outside of browser with Node.js .
  • Node.js: Open-source and class-platform JS runtime environment which means:
  • Node.js is used to build web servers and APIs that handle HTTP requests, as well as web and mobile applications.
  • Browser Node runtime environment
    Primarily for front-end web development so it runs client-side JS. Primarily designed for back-end web development, so it runs server-side JS.
    From browser you can access the DOM API but there are restrictions for accessing the local file system. You can access almost all system resources including file system, but not the DOM.
    Global object is called window which provides access to browser-related functionalities, such as access to Global object is called global which provides access to Node.js specific functionalities.
    No control over the browser environment that your users will use to visit your site. You can chose which node.js version to run.
  • Node.js has become very important for developers worldwide. It has allowed users to use JS both in front-end and back-end of full-stack applications.
    This has made the development process more efficient since developers don't need to learn a new programming language just for developing back-end.
  • Learning Node.js means you can use JS both in front-end and back-end of web applications.
    This means your team doesn't have to switch between languages and they can be more productive and efficient.
  • Node's architecture is non-blocking, event-driven architecture that is great for developing real-time applications, where responsiveness and efficiency are essential for creating a good user experience.
  • Node's architecture relies on a single thread and even loop which means it runs one piece of code at a time.Node's architecture relies of multi-threading.
  • Node has a large number of community around the world and hence there are a lot of learning resource available.
  • npm is Node.js's package manager. It is a powerful tool that allows you to install and manage package and modules for your projects.
  • By using packages you can reuse code that was already written, tested, and shared by other developers to make your workflow faster and more efficient.
  • Node.js is free and open source.
  • Disadvantages of node:
    • Single threaded and so CPU intensive tasks may block the main thread and result in performance issues. For example, complex mathematical operations, image and video processing, cryptography.
    • In asynchronous programming, a task that may take a long time to run is started, but instead of waiting until it's completed, the main program continues running while the asynchronous task runs in the background.
      This often involves what we now know as "callbacks", which are functions that define what happens when the asynchronous operations are completed.
      The asynchronous nature of Node.js can potentially make the code more difficult to read, understand, and debug.
    • Careful when choosing packages from npm because some of them may not be constantly maintained, so they may introduce vulnerabilities into your own application.
  • Node.js is currently one of the most popular tools for developing web applications. Knowing its advantages and disadvantages will help you to determine if it's the right tool for your project.
  • NVM(Node Version Manager) is recommended way to install Node.
  • Notice the --lts flag. This flag indicates that you want to install the current Long-Term Support (LTS) version of Node.js. This version is usually recommended because it prioritizes stability, reliability, and security over new or experimental features. It is thoroughly tested and ready for production. It's also guaranteed to have longer support periods with a focus on bug fixes and security patches.
    • Basic commands of NVM
    • nvm install lts to install the Long-term support version of nvm.
    • nvm use 20 to use the version 20 of node.
    • nvm ls to list all the installed version of node.
    • nvm alias default <version> to set the default version of Node.js on Linux/macOS only
    • nvm alias <new_name> <version> to give a more specific name to a version.
      Note: Probably also only on Mac and Linux.
    • npm installs with node and is the package manager.
    • To run a JavaScript file, node <file.js>
    • npm init to create a Node.js project.
      npm init --yes to start with a default package.json.
    • npm install <package> to install a specific package.
    • npm install to simply install all the dependencies listed in package.json
    Learn Node.js REPL
  • node -e "console.log() will print to console.
    node -p "" evaluates the expression just like console.log above.
  • Use echo to create a file:
    echo "console.log('Hello from a file')" > hello.js
  • Node can check the file for syntax errors without actually running the file with:
    node --check fileName.js
  • Node.js also includes and interactive shell called the REPL - it reads an expression you type, evaluates it, prints the result, and loops back for the next input.
    You can start REPL with node
  • In REPL, _ holds the result of last evaluated expression.
  • REPL has special commands that starts with a dot . . Type .help to see all of them.
  • To leave REPL type .exit or Ctrl+D.
  • There are two ways to import modules in Node, the classic CommonJS style is require() while the ESM, the modern import/export syntax.

   
   

Back-End Development and APIs Certification Exam