Exercise - Write data

Completed

Tailwind Traders is impressed with your APIs, which return existing products. Now you need to create an API that can insert and update products. The data is typically stored in a database and contain millions of records. For that reason, you need to use techniques that limit how much data is passed into the API and returned from the API.

Implement support to write data

Implement the CRUD API on the products resource:

This starter project at nodejs-route\exercise-express-routing\reading-writingcontains the product files and some starter application code. All you need to do is fill in the missing parts.

  1. Right-click the reading-writing folder name and select Open in integrated terminal.

  2. In the terminal, run the following command to install it:

    npm install
    
  3. Open app.js to inspect it. The file should look like this:

     const express = require('express');
     const app = express();
     const port = 3000;
    
     app.use(express.json());
    
     let products = [];
    
     app.post('/products', function (req, res) {
       // implement
     });
    
     app.put('/products', function (req, res) {
       // implement
     });
    
     app.delete('/products/:id', function (req, res) {
       // implement
     });
    
     app.get('/products', (req, res) => {
       // implement
     });
    
     app.listen(port, () =>
       console.log(`Example app listening at http://localhost:${port}`),
     );
    

    The app.js file shows the skeleton of a program. Your next job is to implement the routes.

Implement route to read data

To implement routes, the process is to add a little code and then test it. Continue adding each route's method until you have a fully functional API. Make your changes in the app.js file in the reading-writing directory. All files that start with client are client applications that you can use to test your API.

  1. To support reading from the API, locate the part of the code that looks like this:

    app.get('/products', (req, res) => {
      // implement
    })
    

    Replace it with this code:

    app.get('/products', (req, res) => {
      res.json(products);
    })
    
  2. To check that the code works, start the Node application by running this command:

    node app.js
    
  3. In a separate terminal for the same folder, reading-writing, run the following command. It's important to have two terminal open because you need to run the server and the client at the same time.

    node client-get.js
    

    You should get the following output:

    Received data []
    Connection closed
    

    The API responds with an empty array because you haven't written any data to it yet. Let's change that next.

  4. In the first terminal, select Ctrl+C to stop the application.

Implement route to write data

  1. To implement writing data to in-memory database, locate this code:

    app.post('/products', function (req, res) {
       // implement
    });
    

    Replace it with this code:

    app.post('/products', function(req, res) {
      const newProduct = { ...req.body, id: products.length + 1 }
      products = [ ...products, newProduct]
      res.json(newProduct);
    });
    

    The new code reads incoming data from req.body and constructs a JavaScript object from it. Next, it's added to the products in-memory database array. Finally, the new product is returned to the user.

  2. To test the code, run the server program by running this command:

    node app.js
    
  3. In a separate terminal for the same folder, reading-writing, run the following command. It's important to have two terminal open because you need to run the server and the client at the same time.

    node client-post.js
    

    You should see an output like this:

    response {"name":"product","id":1}
    Closed connection
    
  4. To check that the data is written to the API, run the following command:

    node client-get.js
    

    You should see the following output:

    Received data [{"name":"product","id":1}]
    Connection closed
    

    Your requests so far:

    • client-post.js: The response tells you that when you ran client-post.js, you wrote data to the API.
    • client-get.js: You ran client-get.js to query the API for data. The API responded with the data that you just wrote to it.
  5. In the first terminal, select Ctrl+C to stop the application.

Implement route to update data

  1. To implement the ability to update your data, locate the code that looks like this:

    app.put('/products', function (req, res) {
      // implement
    });
    

    Replace it with this code:

    app.put('/products', function(req, res) {
      let updatedProduct;
      products = products.map(p => {
        if (p.id === req.body.id) {
          updatedProduct = { ...p, ...req.body };
          return updatedProduct;
        }
        return p;
      })
      res.json(updatedProduct);
    });
    

    The new code locates the record in the products in-memory database array that matches the id property, and updates that record.

  2. To test the code, start the server application:

    node app.js
    
  3. In the other terminal, run this command to create a record:

    node client-post.js
    
  4. Run this command to update the newly created record:

    node client-put.js
    

    You should see the following update in the terminal:

    response {"name":"product-updated","id":1}
    Closed connection
    
  5. To check that the update works, run this command:

    node client-get.js
    

    You should see this update:

    Received data [{"name":"product-updated","id":1}]
    Connection closed
    
  6. In the first terminal, select Ctrl+C to stop the application.

Implement route to delete data

  1. To implement deleting, locate the code that looks like this:

    app.delete('/products/:id', function (req, res) {
      // implement
    });
    

    Replace it with this code:

    app.delete('/products/:id', function(req, res) {
      const deletedProduct = products.find(p => p.id === +req.params.id);
      products = products.filter(p => p.id !== +req.params.id);
      res.json(deletedProduct);
    });
    

    The new code finds the product to be deleted. Then it filters out that item from the products in-memory database array and returns the deleted product.

  2. To test the code, start the server application:

    node app.js
    
  3. In a separate terminal, run this command to create a record:

    node client-post.js
    
  4. Run this command to remove the record:

    node client-delete.js
    

    You should see the following output:

    response {"name":"product","id":1}
    Closed connection
    
  5. To check the code, run this command:

    node client-get.js
    

    It should give this output:

    Received data []
    Connection closed
    

    Congratulations! You've implemented a products resource by using a full CRUD (create, read, update, delete actions).

  6. In the first terminal, select Ctrl+C to stop the application.

Cleanup development container

After completing the project, you may wish to clean up your development environment or return it to its typical state.

Deleting the GitHub Codespaces environment ensures that you can maximize the amount of free core hours entitlement you get for your account.

Important

For more information about your GitHub account's entitlements, see GitHub Codespaces monthly included storage and core hours.

  1. Sign into the GitHub Codespaces dashboard (https://github.com/codespaces).

  2. Locate your currently running Codespaces sourced from the MicrosoftDocs/node-essentials GitHub repository.

    Screenshot of all the running Codespaces including their status and templates.

  3. Open the context menu for the codespace and select Delete.

    Screenshot of the context menu for a single codespace with the delete option highlighted.