Advice on snapping an Express.js web server for Ubuntu Core

Hi there,

Does anyone have advice on snapping an Express.js web server for Ubuntu Core? I’m seeing a strange issue where my web server seems to start successfully but it’s responding with a 404 for all static assets.

The application in question is here, snapcraft.yaml here. It’s an extremely simple web server started here with a basic router here.

The only slightly unusual thing about this application is that it’s running in Electron rather than plain Node.js as it also has a graphical front end. (The front end starts up fine).

The server works fine when started via npm on Ubuntu or Mac OS, but when I snap it and install it on Ubuntu Core I get the 404 responses. I wondered whether it might be a strange confinement issue with accessing the filesystem to retrieve static assets, but the assets are all part of the package and it doesn’t work in devmode either.

I’m a bit of a Snapcraft beginner so I’m sure this is something simple, but would be grateful for any advice.

I don’t know anything about express.js, but the first thing I’d look at is whether that relative path in the router (services/static) resolves to the right place.
It’s possible the only thing you need to do is the equivalent of cd $SNAP at the top of the daemon.

Thank you for the tip, I tried changing it to ./services/static (which made no difference), but other than that I’m really struggling to debug this for several reasons:

  1. This is a kiosk snap using xwayland-kiosk-helper as per this tutorial, which only runs on Ubuntu Core and I’ve found it challenging to get it to run on Ubuntu Desktop
  2. Ubuntu Core is running on a Raspberry Pi which requires cross-compiling for armhf. I’ve not found a way to successfully cross-compile locally (I’ve tried docker, lxd and building on a Raspberry Pi running Ubuntu Server) so I’m relying on build.snapcraft.io to generate builds which is a) very slow and b) requires pushing to GitHub every time I make a change!
  3. The immutable nature of snaps means I don’t know how to debug the snap locally on device without generating a new build (which as above requires a whole cycle of commit-push-build-download-install)
  4. I’ve not found any particularly helpful logs to look for errors

I’m sorry this is a bit of a mish-mash of issues but as I said, I’m quite new to Snapcraft and struggling to make progress, so advice on any of the above would be appreciated.

Thanks

You can try using snap try with the .snap file to do this. First take the squashfs file built for that architecture from build.snapcraft.io and copy it to the device, then run unsquashfs <file>.snap and you will have a squashfs-root directory to work with. Running snap try squashfs-root will install the snap from that directory so all you need to do is modify the files in squashfs-root and those changes will show up in real-time in your snap to debug.

Thank you! That was enough to unblock me and I figured out that @chipaca was correct that the problem was the relative path for static files. I think it’s because the snap package executes the Node process from a different directory relative to the static directory than I normally would.

I changed that line to:

router.use(express.static(path.join(__dirname, 'static')));

as recommended by the Express website and it worked.

Thanks again!