Overview
- Backend development refers to the server-side of web development. It involves creating the logic, database interactions, and APIs that power web applications.
- Node.js is an asynchronous event driven JavaScript runtime environment, enabling JS outside of the web browser.
- Created in 2009, Node.js is a popular choice for backend development due to its fast execution, large ecosystem, and single-language development.
Server-side Web Programming
- Web browsers communicate with web servers using HTTP requests.
- The requests includes a URL, a method, and may include additional information via URL params or as POST data or in cookies.
- Server-side code is typically written using web frameworks, which can come in any number of programming languages.
- Server-side programming allows developers to make use of sessions— storing information associated with the current user and tailoring responses.
Static sites vs Dynamic sites
- Static sites serve up the same hard-coded content when a particular resource is requested.
- Dynamic sites dynamically generate content, as requested per user— typically by inserting data from database into HTML templates.
- Most of the code exists on the server. Hence, “server-side programming.”
Backend Web frameworks
- Server-side web frameworks simplify server-side web programming, providing a standard way to build and deploy web applications and offering tools and libraries.
- Key features of web frameworks include:
- Routing: Mapping URLs to specific parts of code.
- Database access and ORM: Simplifying database interaction.
- Session management, security features, caching, and more…
- Common frameworks include Express.js, Django, and Ruby on Rails.
Key Node.js features
- Event-driven, non-blocking I/O model
- Node Package Manager (npm)
- V8 JavaScript engine
- Built-in modules
Runtime Environment
- Node.js provides the necessary components to run JavaScript outside the browser.
- The V8 JavaScript engine (developed by Google for Chrome) compiles and executes JS.
- The runtime provides access to system-level functionality, like file I/O and networking.
Async and Event driven
- Node.js uses an event loop to manage asynchronous operations in a non-blocking manner.
- This allows Node.js to handle many connections concurrently without the overhead of creating a new thread for each connection.
- Node.js registers long running operations as a callback function to be executed later, allowing the program to execute other tasks in them meantime.
- The event loop continuously checks for and dispatches events such as I/O operations, timers, or user interactions.
Getting Started
Node.js Hello World
The below is how to create a simple server use vanilla Node.js. A simpler alternative is to use the popular Express.js framework. See: Overview of Express.js
import { createServer } from 'node:http';
const hostname = '127.0.0.1';
const port = 3000;
const server = createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});Source: https://nodejs.org/en/learn/getting-started/introduction-to-nodejs
EventEmitter
The events module offers the option to utilize the event-driven nature of Node.js.
- Objects can emit events using
EventMitter.emit(). - Code reacts using methods like
EventEmitter.on()orEventEmitter.addListener(). - When an event is emitted, all attached listeners are called synchronously in the order they were registered.
import EventEmitter from 'node:events';
const eventEmitter = new EventEmitter();
eventEmitter.on('start', (number) => {
console.log(`started ${number}`);
});
eventEmitter.emit('start', 23); // "started 23"Source: https://nodejs.org/en/learn/asynchronous-work/the-nodejs-event-emitter