Exercices On Node
Exercices On Node
JS PROGRAMMING LANGUAGE
1. Basics of Node.js
Write a simple Node.js script that prints "Hello, Node.js!" to the console.
Create a script that reads two numbers from the command line arguments and
performs addition, subtraction, multiplication, and division.
Write a Node.js script to display the current date and time using the Date object.
Create a script to read the contents of a text file and display them in the console using
the fs.readFile method.
Write a script to create a new file, write some text into it, and append additional text
without overwriting.
Develop a script to read the contents of a directory and display the names of all files
and folders.
3. HTTP Module
Write a basic HTTP server that listens on port 3000 and responds with "Hello,
World!" when accessed.
Create a server that serves an HTML file when the user accesses the root URL (/).
Build an HTTP server that handles different routes (/, /about, /contact) and sends
appropriate responses.
4. Asynchronous Programming
Write a script that demonstrates the use of callbacks by reading a file and then printing
"File reading completed!" once done.
Create a script that uses Promises to perform a simple asynchronous task (e.g.,
simulate fetching data with a setTimeout).
Develop a script that uses async/await to read the contents of multiple files in
sequence.
5. Event Emitters
Write a script that creates an event emitter, emits a custom event, and listens to it
using on.
Create an event emitter for a "userLoggedIn" event and pass user details (like
username) to the listener.
Develop a script that uses an event emitter to simulate an alarm clock that triggers an
alert at a specific time.
6. Streams
Write a script that creates a readable stream to read the contents of a file and outputs
them to the console.
Create a writable stream that writes user input from the console into a text file.
Develop a script that uses piping to read data from a file and write it to another file.
Create a custom module that exports a function to calculate the factorial of a number
and use it in another file.
Write a script to demonstrate the use of built-in Node.js modules like os (e.g., display
system information).
Build a program that organizes functions into separate files as modules and imports
them into a main script.
Initialize a new Node.js project and install a package (e.g., chalk or lodash).
Demonstrate its usage in your script.
Write a program that uses the axios package to fetch data from a public API and
display it in the console.
Create a script that generates a random password using the crypto package or any
npm library like password-generator.
9. Express.js
Create a basic Express.js server that listens on port 4000 and responds with "Welcome
to Express!" at the root route.
Build an Express application with multiple routes (/, /about, /services) and send
JSON responses for each route.
Write a script to handle POST requests in Express, where the server accepts user data
and logs it to the console.
Write a script to connect to a MongoDB database using the mongoose package and
define a simple schema (e.g., for a User collection with fields like name, email, and
age).
Create a program to insert, update, and delete documents in a MongoDB collection.
Build a script to retrieve and display all records from a MongoDB collection using
Node.js..
Additional Challenges
1. Write a script to implement a real-time chat application using WebSockets with the ws
package or Socket.IO.
2. Build a RESTful API using Express.js that supports CRUD operations for a "Tasks"
resource (e.g., add, view, update, delete tasks).
3. Develop a file upload system using Node.js and Express, where users can upload files,
and the server stores them in a specific directory.
4. Create a server-side pagination system for a large dataset using Node.js and
MongoDB.