DeveloperBreeze

Basic Authentication using 'express-basic-auth' middleware

// Import required modules
const express = require('express');
const app = express();
const basicAuth = require('express-basic-auth');

// Define a user for basic authentication
const users = { 'username': 'password' };

// Use express-basic-auth middleware for authentication
app.use(basicAuth({ users }));

// Define a route accessible only to authenticated users
app.get('/secure', (req, res) => {
  res.send('Authenticated route');
});

Continue Reading

Discover more amazing content handpicked just for you

Tutorial

Build a Custom Rate Limiter in Node.js with Redis

This isn’t just a quick fix—it’s a deep dive into:

  • Atomic operations with Redis
  • Manual request tracking logic
  • Flexibility to customize based on business rules

Apr 04, 2025
Read More
Tutorial
javascript css +1

Building a Real-Time Chat Application with WebSockets in Node.js

npm init -y
npm install express socket.io

Aug 03, 2024
Read More
Code
php

JWT Token Creation and Verification in Node.js using 'jsonwebtoken'

// Import 'jsonwebtoken' module
const jwt = require('jsonwebtoken');

// Secret key for signing and verifying JWT tokens
const secret = 'mysecretkey';

// Create a JWT token with a payload and set expiration to 1 hour
const payload = { user_id: 123, username: 'john_doe' };
const token = jwt.sign(payload, secret, { expiresIn: '1h' });

// Verify a JWT token
try {
    const decoded = jwt.verify(token, secret);
    console.log(decoded);
} catch (error) {
    console.error('Token verification failed.');
}

Jan 26, 2024
Read More
Code
javascript

Simple HTTP Server in Node.js

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Read and Write Files in Node.js using 'fs' module

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Simple RESTful API in Node.js using Express

// Import 'express' module
const express = require('express');
const app = express();

// Define a route for accessing a resource
app.get('/api/resource', (req, res) => {
  res.json({ message: 'Resource data' });
});

// Start the server and listen on port 3000
app.listen(3000, () => {
  console.log('RESTful API is running on port 3000');
});

Jan 26, 2024
Read More
Code
javascript

Date Manipulation and Sum Calculation

// Import required modules
const moment = require('moment');
const _ = require('lodash');

// Get the current date and time using Moment.js
const now = moment();

// Example array of numbers
const numbers = [1, 2, 3, 4, 5];

// Calculate the sum of the array using Lodash
const sum = _.sum(numbers);

Jan 26, 2024
Read More
Code
javascript

Access Command-line Arguments

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Set and Access Environment Variable

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Event Emitter using 'events' module

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Construct File Path using 'path' module

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Create and Print Buffer

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Hashing Password with SHA-256 using 'crypto' module

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Parse URL and Query Parameters

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Execute Shell Command using 'child_process' module

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

File Stream Copy using 'fs' module

// Import 'fs' module
const fs = require('fs');

// Create read and write streams
const readStream = fs.createReadStream('input.txt');
const writeStream = fs.createWriteStream('output.txt');

// Pipe data from read stream to write stream for file copy
readStream.pipe(writeStream);

Jan 26, 2024
Read More
Code
javascript

Simple WebSocket Server using 'ws' library

// Import 'ws' module
const WebSocket = require('ws');

// Create WebSocket Server on port 8080
const wss = new WebSocket.Server({ port: 8080 });

// Event listener for new connections
wss.on('connection', (ws) => {
  // Event listener for incoming messages
  ws.on('message', (message) => {
    console.log('Received:', message);
    // Send a response back to the client
    ws.send('Server: ' + message);
  });
});

Jan 26, 2024
Read More
Code
php

Basic Authentication

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!