Skip to content

Tasks

  1. Install Nodejs 18 LTS in your computer. Verify the installation.

  2. Using the Node.js REPL to create a simple calculator

    1. Open the command prompt or terminal on your computer.
    2. Type node to start the Node.js REPL.
    3. Create a function that takes two numbers as arguments and returns their sum.
    4. Test the function by calling it with different numbers and verifying that the output is correct.
    5. Create additional functions for subtraction, multiplication, and division, and test them in the same way.
    6. Save your code in 702.js file.
  3. Create a command-line program to calculate the area of a rectangle

    1. Use the process.argv array to get the width and height values from the command line.
    2. Parse the width and height values from strings to numbers using the parseInt function.
    3. Calculate the area of the rectangle by multiplying the width and height values together.
    4. 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
    
  4. 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
    
  5. 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:

    1. Create a new Node.js file and require the os and fs modules.
    2. Use the os.platform(), os.arch(), and os.release() methods to get the name of the user's operating system, architecture, and release version, respectively.
    3. Use the os.userInfo() to get the username.
    4. Create a JavaScript object that contains the operating system information and the username.
    5. 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.

  6. Create a Node.js app in order to connect to tienda_informatica database and perform the following basic operations:

    1. Connect to the database, checking if connection is created successfully.
    2. Show in the console a list of products with their price.
    3. Add a new product: Xiaomi Mi Smart Watch Lite, 60€. Show the inserted record.
    4. Change the price of Yoga laptop to 499€. Show the updated record.
    5. Delete the previous created product. Show if the operation is successfully executed.
  7. The aim of this task is to create a custom module and import it.

    1. Copy the task 704 code.
    2. Encapsulate the calculator functionality in an object defined in a separated file calculator.js. 3. Export the module.
    3. Import the module in 707.js.
    4. Adapt the task in order to make it work properly.
  8. Create a Node.js application in order to export the table producto into a valid XML document called productos.xml, the root element must be named products and the document must be formatted.

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

  10. Adjust the two previous tasks to include the vendor data in every product. For instance:

    <product>
         ...
         <name>
              Mi Watch Lite
         </name>
         <vendor>
              <id>1</id>
              <name>Xaomi</name>
         </vendor>
    </product>
    
    This task is optional.

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

  12. Implement the basic server created in Creating applications with Node.js. Then

    1. Create two new files called error404.html and error500.html in public directory.
    2. Change the code in order to serve different pages depending on the request url:
      1. If root (/) is requested index.html page will be served.
      2. If other url are requested error404.html will be served, changing the status code.
      3. If any error are raise error500.html will be served.
    3. Adapt the pages to contain relevant content.
  13. Copy the previous task. Change the HTML files in order to add some dynamic parts:

    1. index.html must show Hello USERNAME if the query username=USERNAME appears in the URL, if not Hello World! must appear.
    2. 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.

  14. Copy the task 713. Refactor it to use EJS template engine.

  15. Do the following tasks:

    1. Copy the previous task.
    2. Create index.ejs. This template will contain the list of products.
    3. The name of the product has to contain a link to /products?id=## where ## is the id of the product.
    4. Modify the server.js code in order to connect to the database and retrieve the products.
    5. Create the products.ejs template that will contain the selected product details.
  16. Add, to the previous task, a search form in order to filter products:

    1. 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).
    2. Change index.js to response to the new page behavior. If no text querystring exists will display all products, if it exists, the page will only display products that meet the search criteria.
  17. 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