Node JS Assignment- 5
Basic Questions
- Write a Node.js script that generates a random 16‑byte string using the crypto module and prints it as a hex value.
- Hash a simple password string and log the hash.
- Compare a plaintext password with a hashed password using bcrypt.compare.
- Create a simple registration script that accepts a username and password and logs them.
- Create a simple login script that validates username and password from an object.
- Use process.env to store a secret key and print it in your script.
- Create a script that logs “Event loop running” every second using setInterval.
- Use console.time and console.timeEnd to measure a function execution time.
- Write a Node.js script that simulates a counter using setInterval, increasing a value by 1 every second, and stops after 10 seconds.
- Write a script that logs process uptime every 2 seconds using process.uptime(), and stops after 5 logs.
- Write a Node.js script that accepts a number from command line arguments and prints whether it is even or odd.
- Write a Node.js script that reads a text file input.txt asynchronously and logs its content.
- Log memory usage of a Node.js application using process.memoryUsage().
- Use worker_threads to run a small computation in a separate thread.
- Cache a small key-value pair in memory using a simple object.
- Read an environment variable to determine the application mode (development or production).
- Log the current Node.js process ID using process.pid.
- Log all active worker threads (if any).
- Print a warning message if a password is less than 8 characters.
- Use console.error to log a simulated security error.
Intermediate Questions
- Implement a simple registration API that hashes passwords before storing them in memory.
- Implement a login API that validates hashed passwords.
- Create a password reset function that generates a temporary token and logs it.
- Implement a rate limiter that prevents too many requests from the same IP.
- Use environment variables to configure database credentials securely.
- Create a script that simulates event loop delay using a CPU-intensive loop.
- Offload a CPU-intensive calculation to a worker thread and log the result.
- Implement in-memory caching for a database query simulation.
- Expire a cached item after 10 seconds using setTimeout.
- Use pm2 to monitor CPU and memory usage of a Node.js process.
- Write a script that logs all uncaught exceptions using process.on(‘uncaughtException’).
- Implement a simple script to sanitize user input to prevent SQL injection.
- Hash multiple passwords asynchronously using bcrypt.hash and log the results.
- Create middleware that blocks requests with missing or invalid authentication tokens.
- Implement a script that logs execution time for multiple asynchronous database queries.
- Create a worker thread that calculates factorial of a large number.
- Store multiple cached values in memory and retrieve them efficiently.
- Implement a login attempt counter to temporarily block a user after 3 failed attempts.
- Log the number of active worker threads periodically.
- Implement a script that reads configuration from .env file using dotenv package.
Advanced Questions
- Build a full login & registration system using hashed passwords stored in a database (MySQL/MongoDB).
- Implement JWT-based authentication and secure routes in Node.js.
- Build a password reset system that generates a secure token, validates it, and updates the password.
- Implement role-based access control for multiple routes in a Node.js application.
- Optimize a CPU-intensive task using worker threads and measure performance improvement.
- Implement database query caching with in-memory cache and automatic invalidation.
- Simulate and handle event loop delays with multiple asynchronous and synchronous tasks.
- Use pm2 to set up cluster mode for a Node.js server and observe load distribution.
- Implement monitoring and logging for all API requests, including execution time, memory usage, and status codes.
- Secure a Node.js application against common attacks like SQL injection, XSS, and request flooding using middleware and validation.