BootCamp by Dr.Angela
1. What is Node.js?
- Node.js is a JavaScript runtime that allows you to run JavaScript outside the browser (on a server or local machine)
- Why use frameworks/libraries? : Reuse components, Reduce development overhead
- ex) Reading files, writing files, URL Strings, Networking, Data Streams, Diagnostics, Tests, Debugger, Error Codes
- Definition : βNode.js is an asynchronous, event-driven JavaScript runtime designed to build scalable network applications.β
- Key Concepts
- JavaScript Runtime : Runs JS outside the browser (server/local computer)
- Asynchronous : Executes tasks without blocking (non-blocking)
- Event-driven : Responds to events (requests, user actions, etc.)
- Application : On Server(Computer)
- Why Node.js? : Full-stack JavaScript, Scalable, Non-blocking I/O , Huge ecosystem
2. Using Node.js
- Check version : node -v
- REPL (Read-Eval-Print Loop) : node
- Commands
- .help : show commands)
- .exit or Ctrl + C (twice) : exit
- Similar to the browser console
- Run a JS file : cd path/to/file node index.js
3. Native Node Modules
- Native Modules : Built-in modules provided by Node.js
- File System (fs) : const fs = require("fs");
- Write file : fs.writeFile("message.txt", "Hello from NodeJS!", (err) => { if (err) throw err; console.log("The file has been saved!"); });
- Read file : fs.readFile("./message.txt", "utf8", (err, data) => { if (err) throw err; console.log(data); });
- "utf8" β encoding option
- Docs : https://nodejs.org/docs/latest-v18.x/api/fs.html
4. NPM (Node Package Manager)
- A repository of packages and libraries
- Website : https://www.npmjs.com/
- Initialize project : npm init
- Quick setup : npm init -y
- Install packages : npm install
- ex) npm i sillyname
- Module Systems
- CommonJS (CJS) : ex) const generateName = require("sillyname");
- ES Modules (ESM) : ex) import generateName from "sillyname";
- Enable ESM : "type": "module"
- Example : import superheroes, { randomSuperhero } from "superheroes";
const name = randomSuperhero();
console.log(`I am ${name}!`);
5. Mini Project: QR Code Generator
- Packages : inquirer(user input), qr-image(generate QR codes)
Install : npm i inquirer qr-image
Note : Be careful with package names (typos can install wrong packages), Always check package documentation
Top comments (0)