Master de II. ULL. 1er cuatrimestre. 2020/2021
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:
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:
rp()
to suspend until the deletion is completed.try/catch
block to handle any errors.Nota: Usa el método rp.delete()
para enviar un request HTTP DELETE
a Elasticsearch.
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:
await
with rp()
to retrieve the bundle object from Elasticsearch.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.