Node

It’s an asynchronous event-driven JS runtime, designed to build scalable network applications

  • Actually not a framework…
  • Node.js is a runtime environment that allows you to run JavaScript code outside of a browser.

Commands

  • node -v
  • Node REPL (Read Eval Print Loop)
    • A computer environment where user inputs are read and evaluated, and then the results are returned to the user
    • similar to JS console in browser.. we’ve just taken that out of the browser and run it in our pc terminal
    • node
      • .help
      • .exit or Ctrl+C
    • (not unique to node, smth we can do with most programming languages)
  • writing a js file and using node to run the entire file
    • node index.js

Native Node Modules

  • Your starting tool set. Has TONS of FEATURES for creating applications (esp backend) to be easier
  • Built-In Libraries/Modules
    • Core Modules: Node.js comes with a rich set of built-in libraries (e.g., http, fs, path, os, stream, etc.) to handle basic tasks like HTTP requests, file system operations, networking, and more, without needing extra libraries.
    • This means you can create basic web servers, manage files, and do networking tasks without needing third-party libraries.
  • NPM (Node Package Manager)
    • NPM is the world’s largest package registry, which means there’s a massive selection of open-source libraries and packages available. You can install packages for anything, like authentication, database integration, utilities, and more.
    • You can use npm to manage dependencies and add functionality to your app easily.
  • cross platform

File System

The native node module that allows us to access the local storage

  • https://nodejs.org/docs/latest-v18.x/api/fs.html
  • remember, with native JS run in the browser, you can’t access a user’s files in their PC
  • but now that we can use JS outside the browser, we can read/write files on the server (or local computer)
  • ways
    • import the code
    • require the bits that we need from this module

require

const fs = require("")
  • module we need needs to be entered as a string
const fs = require("fs");
 
// writes to file
fs.writeFile("message.txt", "Hello node!", (err) => {
  if (err) throw err;
  console.log("File saved!");
});
 
// reads from file
fs.readFile("message.txt", "utf8", (err, data) => {
  if (err) throw err;
  console.log(data);
});
 
  • allows us to take a msg (ex. user input) and write it into a file to be saved onto the computer
  • writeFile, readFile

Node Package Manager (NPM)

  • Native node modules - toolbox we have at home
  • NPM (node package manager) - community tool library
    • its open source and you can search to find all the packages that other developers have created
    • comes pre-bundled with node, so if u have node installed you also have npm installed
  • initializing first npm project
    • npm init
    • this will create the configurations for our project package.json
      • similar to how js objects are structured
  • we can start installing and using npm packages
    • will be added to dependencies in package.json and a separate folder in node_modules
    • npm install <package1> <package2>
    • npm i <package>
    • https://www.npmjs.com/
  • The ECMAScript modules
    • CJS - CommonJS
      • we have to use the “require” keyword
    • ESM - ECMAScript
      • we can use “import” keyword (and whole bunch of behind the scene things)
      • we have to go to package.json and add "type": "module",
      • in the main file, write import generateName from "sillyname";
        • write import xxx from "sillyname"; first, then write the xxx keyword (for autocomplete)
      • you CAN’T mix imports!
  • if u copied some project, the node_modules will not be downloaded for u
    • u have to npm install Example task from angela
/* 
1. Use the inquirer npm package to get user input.
2. Use the qr-image npm package to turn the user entered URL into a QR code image.
3. Create a txt file to save the user input using the native fs node module.
*/
 
import input from "@inquirer/input";
import qr from "qr-image";
import fs from "fs";
 
// getting url from user input
const url = await input({ message: "Enter URL" });
 
// save user input to txt file
fs.writeFile("url.txt", url, (err) => {
  if (err) throw err;
  console.log("file saved!");
});
 
// creating the qr image from user entered url
const qr_img = qr.image(url);
qr_img.pipe(fs.createWriteStream("i_love_qr.png"));