# Node.js grundkurs

Nivå: intermediate | Målgrupp: JavaScript programmerare | Förkunskaper: God erfarenhet av att programmera i JavaScript
https://www.ribomation.se/programmerings-kurser/nodejs/

I den här kursen får du lära dig grunderna i Node.js, såsom streams och event-emitter, samt praktisk
tillämpning av detta och bekantskap med några av de vanligaste ramverken, såsom Express, Jest med flera.

Node.js har på mindre än ett decennium fullständigt ändrat perceptionen av JavaScript och vad det kan användas för.
Plattformen har klart kommit att starkt utmana Java som applikations utvecklings platform.

Ett vanligt begrepp är full-stack development, som innebär användning av JS på browser-client sidan i form av SPA
ramverk som Angular eller Vue, användning av JS på server sidan i form av Express REST web services servrar
och användning av JS/JSON i databasen i form av Firestore med flera.

I den här kursen får du lära dig grunderna i Node.js, såsom streams och event-emitter, samt praktisk
tillämpning av detta och bekantskap med några av de vanligaste ramverken. Vidare, får du också lära
dig om de vanligaste ramverken i Node, såsom Express, Jest, fetch, med flera.

Node.js utvecklas i snabb takt, med två releaser per år, vilket ställer krav på att vara uppdaterad
med det senaste som Node stödjer.

Denna kurs har precis blivit uppdaterad och omarbetad till att innefatta Node version 18. Detta innebär
bland annat att vi i de större programexemplen använder ES modules (ESM), async functions och rent
allmänt använder språket till fullo.

- Understand how Node.js executes
- Write programs with event emitters
- Write programs that handle files
- Implement HTTP clients and servers
- Implement web applications with Express.js
- Implement REST web services with Express.js
- Implement REST web services with Fastify
- Using a MySQL database
- Using a PostgreSQL database
- Perform unit testing with Jest
- Perform unit testing with Vitest
- Handle servers with nodemon

#### Background and Overview
*  History
*  Ryan Dahl
*  Properties
*  Architecture
*  Wrapping the LINUX system API
*  Installation of NodeJS and NPM
*  Running the REPL
*  Running script files
*  NodeJS API documentation

#### 1 - MODULES

#### Common.js Modules
- Modules in Node.js
- Understanding require()
- Usage of built-in (core) modules
- User-defined file modules
- User-defined folder modules
- Modules folder
- Understanding the module concept
- Scope
- IIFE
- The module design pattern
- How to implement a simple require()
- A glims beind Node's own implementation of require()

#### ES Modules (ESM)
- ES versions and Node.js
- What is ES modules?
- ESM support in Node and from which version
- How to apply ESM in modern Node
- Export forms
- Import forms
- Path forms
- Importing core API modules

#### Using TypeScript with Node.js
- Anders Hejlsberg
- Brief about strong typing
- Installation of TS

#### 2 - REMOTE MODULES

#### Using NPM
- Isaac Z Schluter
- What is NPM?
- Installation of a NPM local package
- Installation of a remote NPM package
- Dependencies, app vs. dev
- Useful commands
- Beware of the node-bloat problem
- Publishing a NPM package
- Using a private NPM registry
- Verdaccio
- Alternatives to NPM, such as PNPM and Yarn

#### Node Scripts
- Node shell scripts
- Deployment
- Installation

#### Using NPX
- The problem of globally installed Node scripts
- Install locally and use NPX
- How to install ephemerally and use NPX
- Usage of the package option
- NPX is really NPM exec
- Kick-starting a fresh project
- Script entries in package.json

#### 3 - ASYNCHRONOUS EXECUTION

#### Call-Backs
- How do JS execute?
- The JS event loop
- The JS call-back queue
- The event loop of Node
- Postponed and periodic computation
- The Node API is a wrapper around the Linux API
- Why you cannot translate a C program straight into Node
- Understanding the continuation style

#### Promises
- Limitations of the call-back model of design
- Understanding a JS promise
- Using promises
- Promise chain
- Usage of .then() and .catch()
- How to convert a function taking a call-back into a function returning a promise

#### Async Functions
- Similarity between a promise-chain and a try-catch block
- Async functions in ES2017
- Using async and await
- Async lambda
- Using await in a loop
- Top-level await in ES2020

#### 4 - Emitters and Streams

#### Event Emitters
- What is an event emitter in Node?
- Event types and listeners
- Emitter definition
- Simple emitter
- Receive events
- Receive an event just once
- Dealing with event errors

#### Buffers
- What is a Node buffer?
- Allocating a buffer
- Usage of a buffer
- Slicing a buffer
- Encodings

#### Streams
- What is a Node stream?
- Using .pipe()
- Stream types
- Implementing a Readable
- Implementing a Writable
- Implementing a stream transformer
- Assembly of stream pipeline
- The LineTransformer
- The SplitTransformer
- The Array2ObjectTransformer
- The JsonArrayTransformer
- Useful helper libraries

#### 5 - CORE API

#### Working with Processes
- Child processes in Node
- Using execFile()
- Using exec()
- Using spawn()
- Using fork() and send messages
- Handling POSIX signals
- Sending signals

#### Working with Files
- The fs module
- List of functions with Linux similarities
- Promised based functions
- The path module
- POSIX error codes
- Different ways writing to stdout

#### HTTP Server
- List of built-in network services
- A simple HTTP server
- Serving files
- A simple REST WS server

#### HTTP Client
- Simple GET request
- Handling errors
- Simple POST request, sending data

#### 6 - SELECTED FRAMEWORKS

#### Unit Testing with Jest
- Installation and configuration
- Jest and ESM and how to make it work
- Writing a test function
- Test suite execution
- Understanding matchers
- User-defined matcher
- Testing exceptions
- Testing async code
- Test code coverage
- Life-cycle functions
- Comparison with Vitest

#### HTTP Client with Fetch
- Why use a 3rd party lib for HTTP client access?
- Installation and configuration of node-fetch
- Simple GET request
- Performing non-GET requests
- Sending data
- Simple CRUD REST sequence
- Using json-server

#### HTTP Server with Express
- Why use a 3rd party lib for HTTP server provisioning
- Installation and configuration
- Simple Express app
- Understanding app components
- Defining routes
- Defining filters
- Defining middlewares
- Simple HTML/CSS/JS based web app using Express
- Template engines
- Implementation of a simple template engine
- Using the Express generator
- Simple REST WS server with CRUD operations
- Comparison with Fastify
- Using nodemon

#### Using a MySQL Database
- Installation och configuration of mysql2
- Connecting to a MySQL server
- How to create and drop tables
- How to perform basic CRUD operations
- An Express app using Handlebars template and engine and a MySQL database
- Brief about installing MySQL
- Installing and using MySQL using a Docker container
- Perform similar operations with a PostgreSQL DB

#### 7 - Tools
#### Transpilers
- What is a transpiler?
- JavaScript transpilers
- Installation and usage of Babel
- HTML transpilers
- Usage of Pug
- CSS transpilers
- Usage of SCSS
- Post-processors
- Minification

#### Builders
- What is a build tool?
- Using Gulp

#### Bundlers
- What is a bundler?
- Typical operations of a bundler
- Popular bundlers
- Using Parcel
- Using Vite
