Contents
Node.js
JavaScript on the server: modules and npm, files, streams, processes and an HTTP server without frameworks.
1.Getting started
- What Node.js isWhy Node.js exists, how it differs from the browser and how to install it.
- Running a programHow to create a file with code, run it with the node command and restart it on changes.
- The REPLHow to try code line by line in the interactive mode of Node.js and which commands it has.
- package.json and npm initWhat the package.json file is, how to create it with npm init and which of its fields matter.
- ES modules and CommonJSHow to split a program into files with import and export, and how ES modules differ from CommonJS with require.
- Installing packagesHow to install libraries from npm, how dependencies differ from devDependencies and why package-lock.json exists.
- npm scripts and npxHow to define project commands in the scripts section, run them with npm run and use npx.
2.Program and environment
- Command-line argumentsThe process object and the process.argv array: how to pass data to a program when it starts.
- Exit codesHow a program reports success or failure: process.exitCode, process.exit() and checking the code in the terminal.
- Environment variables and .envProgram settings in process.env, the .env file and loading it with the --env-file flag.
- Paths: node:path and import.meta.dirnameHow to build file paths with the node:path module and find the current module's folder with import.meta.dirname.
3.Files
- Reading and writing filesThe node:fs/promises module: writing a file, reading it, appending to it and handling a missing file.
- Working with foldersHow to create a folder, list files, tell files from folders and delete a folder with node:fs/promises.
- A JSON file as storageHow to keep blog posts in a JSON file: reading, adding a record and saving, moved into a separate module.
- Streams: big files in piecesRead and write streams in Node.js: reading a big file line by line, copying and compressing with pipeline.
4.Asynchrony in Node.js
- The event loop and timersHow Node.js runs asynchronous code: the event loop, setTimeout, setImmediate, process.nextTick and promise-based timers.
- Events and EventEmitterThe EventEmitter class from node:events: subscribing to events, emitting them, one-time handlers and the error event.
- Errors in asynchronous codeHow to catch errors in async functions, what an unhandled promise rejection is and why it stops the program.
5.HTTP without frameworks
- The http module: a first serverYour own web server on the built-in node:http module: request and response, status code, headers and port.
- Routing by handHow to parse the request address with the URL class and pick a response by method and path: post list, post by id, 404.
- Serving static filesHow to serve HTML, CSS and images from a public folder: content type by extension, 404 and protection against escaping the folder.
- The request body and a JSON APIHow to read the body of a POST request in node:http, parse JSON, validate the data and create a post with a 201 response.
- HTTP requests with fetchHow Node.js sends requests to other servers itself: fetch, checking the response, sending JSON and limiting the wait time.
6.Tools and practice
- Child processesHow to start another program from Node.js: execFile for a short result and spawn for long commands with output.
- Worker threadsHow to move a heavy computation to a separate thread with node:worker_threads so the server does not freeze.
- node:crypto basicsHashing, random ids and tokens, and storing passwords with scrypt in the built-in node:crypto module.
- The built-in test runnerAutomated tests without libraries: node:test, node:assert checks and running them with node --test.
- DebuggingHow to find bugs in a Node.js program: useful console methods, the --inspect flag, breakpoints and the editor debugger.
- An SQLite databaseHow to store blog posts in an SQLite database with the built-in node:sqlite module: a table, inserts, queries and parameters.
- A command-line toolHow to write your own console program: parsing flags with parseArgs, asking the user questions with readline and running it by name.
- Deploying to a serverHow to run the blog on a server: installing dependencies, environment variables, the pm2 process manager, logs and a graceful shutdown.