0% found this document useful (0 votes)
12 views3 pages

Exercices On Node

The document contains a series of exercises designed to teach Node.js programming, covering topics such as basic scripting, file system operations, HTTP server creation, asynchronous programming, event emitters, streams, module usage, npm, Express.js, and database interactions. It also includes additional challenges like building a real-time chat application, a RESTful API, a file upload system, and server-side pagination. Each section provides specific tasks to enhance understanding and practical skills in Node.js development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Exercices On Node

The document contains a series of exercises designed to teach Node.js programming, covering topics such as basic scripting, file system operations, HTTP server creation, asynchronous programming, event emitters, streams, module usage, npm, Express.js, and database interactions. It also includes additional challenges like building a real-time chat application, a RESTful API, a file upload system, and server-side pagination. Each section provides specific tasks to enhance understanding and practical skills in Node.js development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EXERCICES ON NODE.

JS PROGRAMMING LANGUAGE

Exercises on Node.js Programming

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.

2. Working with File System (fs module)

 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.

7. Working with Modules

 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.

8. Working with npm

 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.

10. Working with Databases

 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.

You might also like