NodeJS
Node.js is a runtime environment built on Chrome's V8 JavaScript engine, enabling server-side JavaScript development. It provides a non-blocking, event-driven architecture that allows developers to build scalable and efficient network applications. Node.js leverages the power of asynchronous programming, making it particularly suited for building real-time applications and handling a large number of concurrent connections.
Module 1: Introduction to Node.js
What is Node.js?
Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside a web browser.
// Load the HTTP library var http = require('http'); // Create a server and send a response http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World\n'); }).listen(8080);
Understanding Node Package Manager (NPM)
NPM is a package manager for the JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js.
// To install a package npm install <package_name> // To uninstall a package npm uninstall <package_name>
Module 2: Introduction to Express.js
What is Express.js?
Express.js, or simply Express, is a back-end web application framework for Node.js.
// Include express var express = require('express'); var app = express(); // Respond with 'Hello World' for requests that hit our root '/' app.get('/', function (req, res) { res.send('Hello World!'); }); // listen to port 3000 by default app.listen(process.env.PORT || 3000);
Basic Routing with Express
Routing refers to how an application’s endpoints (URIs) respond to client requests.
// Respond with 'Hello World' for requests that hit our root '/' app.get('/', function (req, res) { res.send('Hello World!'); }); // Respond to POST request on the root route ('/'), the application’s home page app.post('/', function (req, res) { res.send('Got a POST request'); });
Module 3: Building RESTful APIs with Express.js
Introduction to RESTful APIs
A RESTful API is an architectural style for an application program interface (API) that uses HTTP requests to access and use data.
app.get('/api/items', (req, res) => { // Return some items }); app.post('/api/items', (req, res) => { // Create a new item });
Working with JSON
JSON (JavaScript Object Notation) is a popular data format with diverse uses in data manipulation and information sending/receiving in Node.js
app.get('/', function (req, res) { res.json({ message: 'Welcome to our API!' }); });
Module 4: Express Middleware
Understanding Middleware
Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle.
app.use(function (req, res, next) { console.log('Time:', Date.now()) next() })
Using Built-in Middleware
Express has a few important built-in middleware functions: json(), urlencoded(), static().
app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use(express.static('public'));