Tasks¶
-
Install Nodejs 18 LTS in your computer. Verify the installation.
-
Using the Node.js REPL to create a simple calculator
- Open the command prompt or terminal on your computer.
- Type
node
to start the Node.js REPL. - Create a function that takes two numbers as arguments and returns their sum.
- Test the function by calling it with different numbers and verifying that the output is correct.
- Create additional functions for subtraction, multiplication, and division, and test them in the same way.
- Save your code in
702.js
file.
-
Create a command-line program to calculate the area of a rectangle
- Use the process.argv array to get the width and height values from the command line.
- Parse the width and height values from strings to numbers using the parseInt function.
- Calculate the area of the rectangle by multiplying the width and height values together.
- Output the area of the rectangle to the console in the following format:
The area of a rectangle with width X and height Y is Z
, where X and Y are the input values and Z is the calculated area.
Here's an example of how your program should behave:
$node rectangle-area.js 4 5 The area of a rectangle with width 4 and height 5 is 20
-
Create a basic command-line calculator:
Adapt
702.js
program in order to ask the user for two numbers and an operator. The program will return the result. The program will continue asking until the user writes "No".Here you have a example of how the program should behave:
$node 704.js prompt: number 1: 1 prompt: number 2: 2 prompt: operation: + The result of 1 + 2 is 3. prompt: Would you like to do another operation?: No
-
Write a program that creates a new text file and saves information about the user's operating system to it.
Prerequisites:
- Working with objects in JavaScript
- Handling JSON strings.
Here are the steps to complete the assignment:
- Create a new Node.js file and require the
os
andfs
modules. - Use the
os.platform()
,os.arch()
, andos.release()
methods to get the name of the user's operating system, architecture, and release version, respectively. - Use the
os.userInfo()
to get the username. - Create a JavaScript object that contains the operating system information and the username.
- Use the
fs.writeFile()
method to create a new file and save the compiled information to it in JSON format.
Here's an example of how the program should behave:
$node 705.js Operating system information saved to os-info.txt
Optionally, if file
os-info.txt
exists ask the user for overwriting it or not. -
Create a Node.js app in order to connect to
tienda_informatica
database and perform the following basic operations:- Connect to the database, checking if connection is created successfully.
- Show in the console a list of products with their price.
- Add a new product: Xiaomi Mi Smart Watch Lite, 60€. Show the inserted record.
- Change the price of Yoga laptop to 499€. Show the updated record.
- Delete the previous created product. Show if the operation is successfully executed.
-
The aim of this task is to create a custom module and import it.
- Copy the task 704 code.
- Encapsulate the calculator functionality in an object defined in a separated file
calculator.js
. 3. Export the module. - Import the module in
707.js
. - Adapt the task in order to make it work properly.
-
Create a Node.js application in order to export the table
producto
into a valid XML document calledproductos.xml
, the root element must be namedproducts
and the document must be formatted. -
Create a Node.js application in order to import product data from the XML file
productos.xml
created in the previous task. Make some changes in their content before importing it. -
Adjust the two previous tasks to include the vendor data in every product. For instance:
This task is optional.<product> ... <name> Mi Watch Lite </name> <vendor> <id>1</id> <name>Xaomi</name> </vendor> </product>
-
Create a web applications that returns an HTML page that includes a h1 heading with the text: Hello world!. In addition, add the requested resouce and the query string. The server must listen to the port 5500.
-
Implement the basic server created in Creating applications with Node.js. Then
- Create two new files called
error404.html
anderror500.html
inpublic
directory. - Change the code in order to serve different pages depending on the request url:
- If root (
/
) is requestedindex.html
page will be served. - If other url are requested
error404.html
will be served, changing the status code. - If any error are raise
error500.html
will be served.
- If root (
- Adapt the pages to contain relevant content.
- Create two new files called
-
Copy the previous task. Change the HTML files in order to add some dynamic parts:
index.html
must showHello USERNAME
if the queryusername=USERNAME
appears in the URL, if notHello World!
must appear.error404.html
must show the requested URL when called.
Tip: You have to make up a way to set a placeholder in HTML files that can be changed in running time.
-
Copy the task 713. Refactor it to use EJS template engine.
-
Do the following tasks:
- Copy the previous task.
- Create
index.ejs
. This template will contain the list of products. - The name of the product has to contain a link to
/products?id=##
where##
is the id of the product. - Modify the
server.js
code in order to connect to the database and retrieve the products. - Create the
products.ejs
template that will contain the selected product details.
-
Add, to the previous task, a search form in order to filter products:
- Modifiy
index.ejs
to include a search form that uses the GET method to send information, when submit button is pressed a new request will be created (/?text=reloj
, for instance). - Change
index.js
to response to the new page behavior. If notext
querystring exists will display all products, if it exists, the page will only display products that meet the search criteria.
- Modifiy
-
Create a form in order to create new products. The form will be displayed by requesting
/products/new
and it will require all fields to insert a new product to database. To learn how to process form data in Node.js you can check: How to handle the post request body in Node.js