Node JS Assignment- 2

Basic Questions

  1. Print the value of __dirname in a Node.js script.
  2. Print the value of __filename in a Node.js script.
  3. Log the global object using console.log(global).
  4. Print a specific environment variable (e.g., NODE_ENV) using process.env.
  5. Access and print all command-line arguments using process.argv.
  6. Create a timer using setTimeout to print “Hello after 2 seconds”.
  7. Create an interval using setInterval that prints “Repeating task” every 3 seconds.
  8. Use setImmediate to print “Immediate execution”.
  9. Use console.warn to print a warning message.
  10. Use console.error to print an error message.
  11. Use console.time and console.timeEnd to measure the time taken by a simple loop.
  12. Require the built-in os module and print the OS platform.
  13. Require the built-in path module and print the directory name of a file path.
  14. Create a simple custom module that exports a function returning “Hello World” and import it.
  15. Install a small npm package (like chalk) and print colored text.
  16. Use the fs module to check if a file exists.
  17. Log the keys of the process object.
  18. Use crypto module to generate a random byte buffer and print it.
  19. Create a simple event emitter that prints “Event triggered!”.
  20. Listen to a custom event “greet” and print “Hello Event”.

Intermediate Questions

  1. Write a script that prints the number of command-line arguments passed.
  2. Write a script that prints the 3rd argument passed via the command line.
  3. Use setTimeout to print “First” after 1 second and “Second” immediately to demonstrate asynchronous behavior.
  4. Use setImmediate and setTimeout together to observe execution order.
  5. Build a custom module that exports a class Calculator with add and subtract methods.
  6. Import and use your Calculator module to add two numbers.
  7. Use fs.readFile to asynchronously read a file and log its content.
  8. Use fs.readFileSync to synchronously read the same file and compare execution.
  9. Use the path module to join directory and file name dynamically.
  10. Use the os module to print total system memory and free memory.
  11. Create a simple Node.js HTTP server using the http module that returns “Hello Node.js Core”.
  12. Write a script that chains two events: “start” triggers “process” using events.
  13. Handle an error event using a custom event emitter.
  14. Use npm to install moment package and print the current date in a formatted way.
  15. Write a script that prints the filename and directory name of the current module dynamically using path.
  16. Create a function exported from a module that converts a string to uppercase and use it.
  17. Build an event emitter that prints “User joined” when “join” event is triggered.
  18. Create a script that prints Node.js memory usage using process.memoryUsage().
  19. Write a small script that prints the CPU architecture and number of cores using os module.
  20. Create a module that exports multiple functions and import them using destructuring. 

Advanced Questions

  1. Build a Node.js program demonstrating chained custom events: Event A triggers Event B triggers Event C.
  2. Write a script that handles errors in an event emitter without crashing the program.
  3. Use fs.watch to monitor a directory and emit a custom event when a new file is added.
  4. Build a modular Node.js project with at least 3 custom modules interacting with each other.
  5. Write a Node.js program to demonstrate asynchronous vs synchronous execution using timers and file operations.
  6. Create a reusable custom module that exports a class with private properties using Node.js module pattern.
  7. Build a Node.js script that uses events to simulate a real-time notification system.
  8. Write a Node.js program that uses process.argv to take input for file paths and reads them asynchronously.
  9. Implement a Node.js script that calculates and prints execution time for multiple asynchronous operations using console.time.
  10. Build a small project demonstrating CommonJS modules, importing both built-in and custom modules, and using npm package dependencies together.