Node JS Assignment- 2
Basic Questions
- Print the value of __dirname in a Node.js script.
- Print the value of __filename in a Node.js script.
- Log the global object using console.log(global).
- Print a specific environment variable (e.g., NODE_ENV) using process.env.
- Access and print all command-line arguments using process.argv.
- Create a timer using setTimeout to print “Hello after 2 seconds”.
- Create an interval using setInterval that prints “Repeating task” every 3 seconds.
- Use setImmediate to print “Immediate execution”.
- Use console.warn to print a warning message.
- Use console.error to print an error message.
- Use console.time and console.timeEnd to measure the time taken by a simple loop.
- Require the built-in os module and print the OS platform.
- Require the built-in path module and print the directory name of a file path.
- Create a simple custom module that exports a function returning “Hello World” and import it.
- Install a small npm package (like chalk) and print colored text.
- Use the fs module to check if a file exists.
- Log the keys of the process object.
- Use crypto module to generate a random byte buffer and print it.
- Create a simple event emitter that prints “Event triggered!”.
- Listen to a custom event “greet” and print “Hello Event”.
Intermediate Questions
- Write a script that prints the number of command-line arguments passed.
- Write a script that prints the 3rd argument passed via the command line.
- Use setTimeout to print “First” after 1 second and “Second” immediately to demonstrate asynchronous behavior.
- Use setImmediate and setTimeout together to observe execution order.
- Build a custom module that exports a class Calculator with add and subtract methods.
- Import and use your Calculator module to add two numbers.
- Use fs.readFile to asynchronously read a file and log its content.
- Use fs.readFileSync to synchronously read the same file and compare execution.
- Use the path module to join directory and file name dynamically.
- Use the os module to print total system memory and free memory.
- Create a simple Node.js HTTP server using the http module that returns “Hello Node.js Core”.
- Write a script that chains two events: “start” triggers “process” using events.
- Handle an error event using a custom event emitter.
- Use npm to install moment package and print the current date in a formatted way.
- Write a script that prints the filename and directory name of the current module dynamically using path.
- Create a function exported from a module that converts a string to uppercase and use it.
- Build an event emitter that prints “User joined” when “join” event is triggered.
- Create a script that prints Node.js memory usage using process.memoryUsage().
- Write a small script that prints the CPU architecture and number of cores using os module.
- Create a module that exports multiple functions and import them using destructuring.
Advanced Questions
- Build a Node.js program demonstrating chained custom events: Event A triggers Event B triggers Event C.
- Write a script that handles errors in an event emitter without crashing the program.
- Use fs.watch to monitor a directory and emit a custom event when a new file is added.
- Build a modular Node.js project with at least 3 custom modules interacting with each other.
- Write a Node.js program to demonstrate asynchronous vs synchronous execution using timers and file operations.
- Create a reusable custom module that exports a class with private properties using Node.js module pattern.
- Build a Node.js script that uses events to simulate a real-time notification system.
- Write a Node.js program that uses process.argv to take input for file paths and reads them asynchronously.
- Implement a Node.js script that calculates and prints execution time for multiple asynchronous operations using console.time.
- Build a small project demonstrating CommonJS modules, importing both built-in and custom modules, and using npm package dependencies together.