Node.js's cluster module, which allows for the creation of multiple instances of a Node.js application to utilize multiple CPU cores effectively.
Cluster is following round-robin approach.
Workload is equally distribute in our servers and Handling traffic.
Example of Cluster with Node.jsconst express = require('express');const...
Showing posts with label Node.js. Show all posts
Showing posts with label Node.js. Show all posts
Friday, 27 December 2024
Thursday, 19 December 2024
undefined
undefined
Node.js handle uncaught exceptions Node.js handle uncaught exceptions Node.js handle uncaught exceptions
const express = require('express');const app = express();// Middleware to parse JSON requestsapp.use(express.json());// A sample routeapp.get('/', (req, res) => { res.send('Welcome to the...
undefined
undefined
Node.js with Sample : Mock API using expressconst express = require('express');const app = express();const PORT = 3000;// Mock dataconst users = [ { id: 1, name: 'John Doe', email: 'john.doe@example.com' }, { id: 2, name: 'Jane Smith', email: 'jane.smith@example.com' }, { id: 3, name: 'Sam Johnson',...
undefined
undefined
Express using create server.const express = require('express');const app = express();// Middleware to parse JSON body dataapp.use(express.json());// Custom Middleware to Modify Request Dataapp.use((req, res, next) => { if (req.body && typeof req.body === 'object') { // Add a new property to...
undefined
undefined
In Node.js, you can create a server using the built-in http module.
const http = require('http');const server = http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello, World!\n');});const port = 3000;server.listen(port, () => { console.log(`Server...
Tuesday, 17 December 2024
undefined
undefined
In Node.js, middleware is a function that acts as an intermediary between software layers to process incoming requests and outgoing responses.const express = require('express');const app = express();const middleFunc = (req, res, next) => { req.customText = "Test middleware function..."; next();}app.use(middleFunc);app.get('/',...
undefined
undefined
The event-driven architecture allows asynchronous/synchronous programming, and your application becomes able to perform non-blocking/blocking operations.
EventEmitter class is part of the events module in Node.js.
EventEmitter Methods : .on, .emit (Triggers the event), .once(executes only once), removeListener(Removes...