The Smallest Host to Run a CAPM-based Node.js Application
I will not explore highly specialized platforms – instead, I’m going to put under test an affordable, general purpose, lightweight single-board computer – Raspberry Pi. To make a test more challenging, I’m not going to use the latest, more advanced and powerful models (such as Raspberry Pi 3), but will use the smallest and the least powerful model – Raspberry Pi Zero W. This is a very tiny microcomputer – it is smaller than a credit card (is dimensions are 65 mm x 30 mm x 5 mm), its weight is just 9 g and its price at the time of writing of this blog is below £10:
Hold on for a moment. Before we progress further and step into setup and configuration details, you might be wondering about practical usage of such a setup and rationale behind running this kind of Node.js applications on Raspberry Pi, especially considering that the application could have been set up and executed locally on a laptop or pushed to a (trial) account of SAP Cloud Platform and started there in a matter of few clicks. Don’t get bothered about that – there is no practical usage (or at least not anything close to it that I could think of – if you find one, please share it in comments) – it all has been done just for fun, and the only purpose was to see if a tiny single-board computer can cope with running a simple cloud-ready Node.js application created on basis of CAPM principles in the days of powerful developer laptops and widely used cloud platforms. And you should remember about a challenge component that was set earlier – we are looking for a smallest possible and yet affordable platform, not the most powerful, scalable, feature-rich, etc. With this in mind, let’s move on to technical aspects now.
Operating system and network access setup
Since Raspberry Pi is shipped as bare metal, we firstly need to install an operating system. I use Raspbian Stretch – to be more precise, its minimal image – Raspbian Stretch Lite, so that Raspberry Pi can be operated in a headless mode. I followed standard instructions available at the manual on installation of operating system images to write an image.
The next step is to connect Raspberry Pi to a Wi-Fi network and enable SSH on it so that it can be accessed from a laptop – configuration steps are described at the manual on setting up a Raspberry Pi headless.
Upon completion of above steps, Raspberry Pi can be powered on and if everything has been set up correctly and the device got connected to the network, Raspberry Pi shall become discoverable, and we can proceed to setting up SSH connection, remotely logging into Raspberry Pi, changing a default password for a built-in user ‘pi’, and running the rest of configuration steps described below using the terminal.
Environment and runtime setup
Let’s start from installing Git – this will be handy when cloning a repository that contains the Node.js application later:
sudo apt install git
This step above is optional – we can omit it and obtain application in a number of alternative ways: clone a repository to a laptop and copy it from a laptop to Raspberry Pi via SSH (SFTP), download content of a master branch of a repository in a ZIP file using cURL and unzip it.
Next, we need to install Node.js runtime. I’m going to install latest compatible version, so I will not install it from a repository, but will go through manual installation.
Before downloading Node.js distribution, we shall figure out which distribution we are going to use. To do so, we firstly check CPU architecture of Raspberry Pi:
As it can be seen, this model of Raspberry Pi uses ARM v6 architecture. We can now navigate to a list of Node.js distributions and find a compatible one. Node.js v12 requires at least ARM v7, so it is not compatible with Raspberry Pi Zero W, whereas Node.js v11 is compatible with this Raspberry Pi model. I plan to use the latest compatible distribution, so we are going to download and install Node.js v11.15.0:
curl -O https://nodejs.org/dist/latest-v11.x/node-v11.15.0-linux-armv6l.tar.xz
Remaining steps are a simplified version of instructions available at the manual on installation of Node.js via binary archive:
sudo mkdir -p /usr/local/lib/nodejs
sudo tar -xvf node-v11.15.0-linux-armv6l.tar.xz --strip-components=1 -C /usr/local/lib/nodejs
Add location of Node.js binaries to an environment variable PATH permanently by appending user specific shell initialization script (~/.profile):
Before diving into subsequent steps of installation procedure, given it will involve installation of global Node.js modules, we need to ensure that the user has sufficient privileges for Node.js binaries and global modules location – we can change privileges to a corresponding directory and its subdirectories, or alternatively, change ownership for it:
sudo chown -R $USER /usr/local/lib/nodejs
Node.js runtime is now installed and ready. Optionally, we can install tools for system monitoring – such as package sysstat or nmon – to complement built-in tools for a more comprehensive system workload analysis.
Remaining steps are to download and install the Node.js application – for this, we are going to use installation instructions available at GitHub repository of the application.
Firstly, CDS module is installed globally from SAP NPM registry:
npm set @sap:registry=https://npm.sap.com
npm install -g @sap/cds
Next, obtain, install and deploy the Node.js application:
git clone https://github.com/sapmentors/sitregcapm.git
npm run build
npm run deploy
Finally, we are ready to start the Node.js application:
The application is up and running – and we can now access it remotely (for example, from the laptop) using a browser or an HTTP client to execute queries to its APIs: