Creating a serverless application that uses OCLC APIs

In my previous post, I provided an overview of serverless infrastructure and how it can facilitate development and deployment of applications. In this post, I’ll discuss creating a serverless web application that accessing the WorldCat Metadata API. You can see the full application code in the Developer Network GitHub repository.

Application Basics

The goal of this application is to allow a user to view a bibliographic record by OCLC Number. The application has a form which allows an OCLC Number to be input. A call is then made to the WorldCat Metadata API for data. Then the application displays the record returned to the user. The application is written in Node.js and uses the Express framework for routing and MVC (Model, View, Controller) functionality.

Loading Dependencies and Configuration

In order for the application to function, the proper dependencies and configuration needs to be loaded. Dependencies are specified in the package.json file and installed using npm (node package manager). Once the dependencies are installed, they can be used in the code using the following syntax

const axios = require("axios");

In addition to the dependencies, the application needs basic configuration information in order to access the WorldCat Metadata API. Application configuration information is stored in the config.yml file. This data is then loaded so the application can access and use it.

global.config = yaml.load(config.yml);

Once the dependencies and configuration data are loaded, the foundational pieces of the application are in place.

Obtaining and Parsing Data

The next piece of the application which needs to be built is the model code which interact with data via the WorldCat Metadata API.

Model

The purpose of the model code is to perform interactions with the data object(s) the application consumes. In the case of this application, the Bib.js class performs tasks such as retrieving a bibliographic record via the WorldCat Metadata API, loading the data from that record into a bib object, and making the data available to the application.
The key method in the class is the “find” method which calls the WorldCat Metadata API and retrieves a bibliographic record. Once the response is retrieved the application parses it, captures the MARC record data in a bib object, and returns the bib object. The class also includes methods to return the raw MARC, and specific data within the MARC such as the OCLC Number, Title, and Author.

The model also contains a BibError class which handles error information that the API might return and allows specific error data to be accessed from the application. This class is used within the application to display error messages to the user and log errors for review.

Displaying Data

Views

The purpose of views is to create the displays within the application. To create a consistent experience the application uses two include files:

  • header.html
  • footer.html

The application uses these include files within its two basic views:

  • index.html
  • display-bib.html

The index view renders a form for the user to input an OCLC Number. The display-bib view shows the user a specific bibliographic record. In addition to the two basic views, the application also contains a view to display error messages – display-error.html.

Route handler

The model and view portion of the application code are joined together in the router handler piece of the application code. The purpose of the route handler is to establish the URL patterns and methods that are available in the application and what happens when those URL patterns and methods are invoked. This application has three routes:

  • GET /
  • POST /bib
  • GET /bib/:id

The first route renders a form into which an OCLC number can be entered and submitted. The second route processes the POSTed form data—including an OCLC Number—makes an API request for the corresponding bibliographic record, and displays the data returned to the screen. The third route takes the “id” portion of the route URL, makes an API request for the corresponding bibliographic record, and displays the data returned to the screen.

Next steps

In this post, we’ve looked at how to use serverless to interact with OCLC’s APIs. In our next post, we’ll look at keeping credentials secure in a serverless application.

  • Karen Coombs

    Karen Coombs

    Senior Product Analyst