Using the CloudRail Node.js SDK with Electron

 In CloudRail, Tutorial

Electron is a framework for creating native applications with web technologies like JavaScript, HTML, and CSS by providing a runtime with native operating system APIs. It is a variant of Node.js focused on desktop applications instead of servers and can thus use Node.js libraries.
Consequently it is not hard to use the CloudRail SI Node.js SDK in Electron based applications and we will discuss how it’s done. We will create a small app that gets a user’s authorization to access their Dropbox account and then presents information about available space. From there on it is easily extended to work with all the other functionalities CloudRail SI provides.

How it works

Every electron app requires at least a package.json file for metadata, a main.js file for the background logic and an index.html for the graphical content. We will also add a renderer.js file which is referenced in the index.html to cleanly separate HTML and JavaScript.

Metadata

The package.json shoud look like this:

This is mostly the general information a Node.js application should have. In addition, we note in particular the main entry which references our background logic file, a startup shortcut and the dependency to the CloudRail SI library.

Background logic

The main.js has the following content:

This file will initialize our app by creating a new View and loading the contents of the index.html into it.

View

The index.html has some simple HTML:

Most importantly it defines two buttons to click, a pane to display text and references the renderer.js file.

View logic

The JavaScript to be executed in the View is defined in the renderer.js:

This file is where the interesting stuff happens. The CloudRail SI library is imported and the Dropbox service initialized. The use of the method getElectronAuthenticator is the main distinction point from a non-Electron node.js app, it makes sure authentication is neatly integrated into the View. The rest of the code essentially registers click listeners on the two buttons which trigger the interaction with the Dropbox service. The code using the localStorage, saveAsString and loadAsString makes sure that a user does not have to reauthenticate after restarting the app since credentials are persisted.

Summary

That’s it, you can execute the app with the usual npm install && npm start and have it running. That’s what it should look like:

As you can see it’s really easy to use CloudRail with electron apps to enrich them with a plentitude of useful API integrations. Have a look at the CloudRail SI Node.js SDK’s documentation to learn about all the other methods and services you can use.

Recent Posts