Sistemas y Tecnologías Web: Servidor

Master de II. ULL. 1er cuatrimestre. 2020/2021


Organization ULL-MII-SYTWS-2021   Classroom ULL-MII-SYTWS-2021   Campus Virtual SYTWS   Chat Chat   Profesor Casiano

Table of Contents

Práctica: Developing RESTful Web Services (p11-t3-restful)

Lea el Chapter 7. Developing RESTful Web Services del libro de Jim Wilson. 2018 (Debes estar autenticado via PuntoQ BULL) y lea también las notas del profesor sobre este capítulo.

En este capítulo se crea - utilizando Express.js - un servicio Web que permite crear y administrar colecciones de libros que en el libro denominan bundles.

Lea el capítulo y resuelva los problemas planteados en la secciones:

Deleting a Bundle Entirely

La tarea es añadir una entrada al fichero web-services/b4/lib/bundle.js para suprimir un bundle o colección:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 app.delete("/api/bundle/:id", async (req, res) => {
    /**
    * Delete a bundle entirely.
    * curl -X DELETE http://<host>:<port>/api/bundle/<id>
    */
    const options = {
      ...
    };
    try {
      ...
    } catch(esResErr) {
      ...
    }
  });

Inside the Express route handler callback function, you should do the following:

  • Determine the bundle’s URL based on the es config object and the request parameters.
  • Use await with a call to rp() to suspend until the deletion is completed.
  • Wrap your await call in a try/catch block to handle any errors.

Nota: Usa el método rp.delete() para enviar un request HTTP DELETEa Elasticsearch.

Removing a Book from a Bundle

La tarea consiste en añadir en el fichero web-services/b4/lib/bundle.js una entrada para suprimir un libro de una determinada colección/bundle.

1
2
3
4
5
6
7
8
9
10
11
12
  app.delete("/api/bundle/:id/book/:pgid", async (req, res) => {
    /**
    * Remove a book from a bundle.
    * curl -X DELETE http://<host>:<port>/api/bundle/<id>/book/<pgid>
    */
    const bundleUrl = `${url}/${req.params.id}`;
    try {
      ...
    } catch (esResErr) {
      res.status(esResErr.statusCode || 502).json(esResErr.error);
    }
  });

Inside the try{} block you’ll need to do a few things:

  • Use await with rp() to retrieve the bundle object from Elasticsearch.
  • Find the index of the book within the bundle.books list.
  • Remove the book from the list. Hint: use Array.splice().
  • PUT the updated bundle object back into the Elasticsearch index, again with await and rp().

Note that if the bundle doesn’t contain the book whose removal is being requested, your handler should return a 409 Conflict HTTP status code. You can make this happen by throwing an object with a statusCode property set to 409 and an error object that contains information about what went wrong. This will be caught by the catch block and used to finish the Express response. If you get stuck, check out the code with the solutions.

Reto

Recursos

Comment with GitHub Utterances

Comment with Disqus

thread de discusion