Node JS Assignment- 5

Basic Questions

  1. Write a Node.js script that generates a random 16‑byte string using the crypto module and prints it as a hex value.
  2. Hash a simple password string and log the hash.
  3. Compare a plaintext password with a hashed password using bcrypt.compare.
  4. Create a simple registration script that accepts a username and password and logs them.
  5. Create a simple login script that validates username and password from an object.
  6. Use process.env to store a secret key and print it in your script.
  7. Create a script that logs “Event loop running” every second using setInterval.
  8. Use console.time and console.timeEnd to measure a function execution time.
  9. Write a Node.js script that simulates a counter using setInterval, increasing a value by 1 every second, and stops after 10 seconds.
  10. Write a script that logs process uptime every 2 seconds using process.uptime(), and stops after 5 logs.
  11.  Write a Node.js script that accepts a number from command line arguments and prints whether it is even or odd.
  12. Write a Node.js script that reads a text file input.txt asynchronously and logs its content.
  13. Log memory usage of a Node.js application using process.memoryUsage().
  14. Use worker_threads to run a small computation in a separate thread.
  15. Cache a small key-value pair in memory using a simple object.
  16. Read an environment variable to determine the application mode (development or production).
  17. Log the current Node.js process ID using process.pid.
  18. Log all active worker threads (if any).
  19. Print a warning message if a password is less than 8 characters.
  20. Use console.error to log a simulated security error.

Intermediate Questions

  1. Implement a simple registration API that hashes passwords before storing them in memory.
  2. Implement a login API that validates hashed passwords.
  3. Create a password reset function that generates a temporary token and logs it.
  4. Implement a rate limiter that prevents too many requests from the same IP.
  5. Use environment variables to configure database credentials securely.
  6. Create a script that simulates event loop delay using a CPU-intensive loop.
  7. Offload a CPU-intensive calculation to a worker thread and log the result.
  8. Implement in-memory caching for a database query simulation.
  9. Expire a cached item after 10 seconds using setTimeout.
  10. Use pm2 to monitor CPU and memory usage of a Node.js process.
  11. Write a script that logs all uncaught exceptions using process.on(‘uncaughtException’).
  12. Implement a simple script to sanitize user input to prevent SQL injection.
  13. Hash multiple passwords asynchronously using bcrypt.hash and log the results.
  14. Create middleware that blocks requests with missing or invalid authentication tokens.
  15. Implement a script that logs execution time for multiple asynchronous database queries.
  16. Create a worker thread that calculates factorial of a large number.
  17. Store multiple cached values in memory and retrieve them efficiently.
  18. Implement a login attempt counter to temporarily block a user after 3 failed attempts.
  19. Log the number of active worker threads periodically.
  20. Implement a script that reads configuration from .env file using dotenv package.

Advanced Questions

  1. Build a full login & registration system using hashed passwords stored in a database (MySQL/MongoDB).
  2. Implement JWT-based authentication and secure routes in Node.js.
  3. Build a password reset system that generates a secure token, validates it, and updates the password.
  4. Implement role-based access control for multiple routes in a Node.js application.
  5. Optimize a CPU-intensive task using worker threads and measure performance improvement.
  6. Implement database query caching with in-memory cache and automatic invalidation.
  7. Simulate and handle event loop delays with multiple asynchronous and synchronous tasks.
  8. Use pm2 to set up cluster mode for a Node.js server and observe load distribution.
  9. Implement monitoring and logging for all API requests, including execution time, memory usage, and status codes.
  10. Secure a Node.js application against common attacks like SQL injection, XSS, and request flooding using middleware and validation.