Adding a NodeJS backend to Uberspace

Posted on Jun 28, 2022
Note: This article was written a while ago and may contain outdated information. Please verify the details before relying on it. If I express opinions or recommendations, they might not reflect my current views. For this reason, I recommend checking for more recent articles on the same topic.

Recently I added a NodeJS backend to my Uberspace webspace, which can be controlled by start/stop/logs scripts.

This can be useful if you want to serve an API via your Uberspace account.

Here is how I did it.

Note: For these examples dennis is the username of the uberspace. Just change it.

Install dependencies

If you need to run for example a NestJS application instead of a pure node application, then install it globally. For NestJS it would be:

npm install -g @nestjs/cli

However, you need to make it possible to run your start command via the CLI!

Upload files

First I created a file in my /home/dennis directory:

mdkir api

Then I uploaded my application.

Create .ini file

To control the API via start/stop/logs scripts and make it running in the background, create a .ini file for supervisord:

cd /home/dennis/etc/services.d
nano api.ini

Now add your start commands to it:

[program:api]
directory = /home/dennis/api
command = node index.js
startsecs = 20
stopasgroup=true
stopsignal=QUIT

Explanation of the .ini file

Let’s have look line by line:

[program:api] Is the first line and api is the name under which you will control the program. The name must be unique!

directory = /home/dennis/api This is the root directory (or for supervisord: working directory) of your API.

command = node start This is the command, which shall be executed by supervisord. You could also write nest start for a NestJS application. Beware: When you start it with npm run start you cannot be sure if your API will do a full stop/restart when needed, as it can be possible that some orphaned processes are still running. See stopasgroup to prevent it.

startsecs = 20 The time for how long supervisord shall wait until your application starts.

stopasgroup=true Will also quit sub-processes on the quit signal.

stopsignal=QUIT Which signal shall be used when you call supervisorctl stop.

Add scripts

You can create shell files and make them executable via chmod +x script.sh.

This will make it a little bit easier for you to start/stop and restart your application.

Here are some useful scripts for that:

start.sh

#!/bin/bash

supervisorctl start api

stop.sh

#!/bin/bash

supervisorctl stop api

status.sh

#!/bin/bash

supervisorctl status

log.sh

#!/bin/bash

supervisorctl tail api stderr

restart.sh

#!/bin/bash

supervisorctl restart api

Add web backend

If you want to make your web backend available from the outside through your domain, then setup the domain and a web backend.

Here just a few of lot examples.

On a specific path

This will make your API, running on Port 3000 available on your URL with the path /api:

uberspace web backend set /api --http --port 9000

On a specific domain

This will make your API, running on Port 3000 available on the URL api.example.com:

uberspace web backend set api.example.com --http --port 3000

Resources