// 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);Date Manipulation and Sum Calculation
Continue Reading
Discover more amazing content handpicked just for you
Build a Custom Rate Limiter in Node.js with Redis
// redisClient.js
const redis = require("redis");
const client = redis.createClient({ url: process.env.REDIS_URL });
client.on("error", (err) => console.error("Redis error:", err));
client.connect();
module.exports = client;// rateLimiter.js
const client = require("./redisClient");
const rateLimiter = (limit = 100, windowSec = 3600) => {
  return async (req, res, next) => {
    const ip = req.ip;
    const key = `rate_limit:${ip}`;
    const current = await client.get(key);
    if (current !== null && parseInt(current) >= limit) {
      return res.status(429).json({ error: "Too many requests. Try later." });
    }
    const multi = client.multi();
    multi.incr(key);
    if (!current) {
      multi.expire(key, windowSec);
    }
    await multi.exec();
    next();
  };
};
module.exports = rateLimiter;JavaScript Utility Libraries Cheatsheet
<table> <tr> <th>Function</th> <th>Description</th> <th>Example</th> </tr> <tr> <td><code>R.compose(...functions)Composes functions from right to left. R.compose(Math.abs, R.add(1))(-5)=>4R.curry(fn)Returns a curried version of the provided function. R.curry((a, b) => a + b)(1)(2)=>3R.filter(predicate, list)Returns a new list containing only the elements that satisfy the predicate. R.filter(x => x % 2 === 0, [1, 2, 3, 4])=>[2, 4]R.map(fn, list)Applies the function to each element of the list. R.map(x => x * 2, [1, 2, 3])=>[2, 4, 6]R.reduce(reducer, initialValue, list)Reduces the list to a single value using the reducer function. R.reduce(R.add, 0, [1, 2, 3])=>6
Date-fns is a modern JavaScript date utility library that provides over 200 functions for manipulating dates without modifying native JavaScript objects.
Building a Real-Time Chat Application with WebSockets in Node.js
npm init -ynpm install express socket.ioJWT Token Creation and Verification in Node.js using 'jsonwebtoken'
No preview available for this content.
Simple HTTP Server in Node.js
No preview available for this content.
Read and Write Files in Node.js using 'fs' module
No preview available for this content.
Simple RESTful API in Node.js using Express
No preview available for this content.
Access Command-line Arguments
No preview available for this content.
Set and Access Environment Variable
No preview available for this content.
Event Emitter using 'events' module
No preview available for this content.
Construct File Path using 'path' module
No preview available for this content.
Basic Authentication using 'express-basic-auth' middleware
No preview available for this content.
Hashing Password with SHA-256 using 'crypto' module
// Import 'crypto' module
const crypto = require('crypto');
// Example password
const password = 'mypassword';
// Create SHA-256 hash of the password
const hash = crypto.createHash('sha256').update(password).digest('hex');
// Log the generated hash
console.log('Hash:', hash);Parse URL and Query Parameters
No preview available for this content.
Execute Shell Command using 'child_process' module
No preview available for this content.
File Stream Copy using 'fs' module
No preview available for this content.
Simple WebSocket Server using 'ws' library
No preview available for this content.
Date Formatting for Specific Date ('Y-m-d')
No preview available for this content.
Discussion 0
Please sign in to join the discussion.
No comments yet. Start the discussion!