Solved

How to add health check endpoint for global load balancing

aviita
Shopify Partner
7 0 0

We have created an app with node template using:
```
npm init @Shopify/app@latest
```

It seems to have been made exceptionally hard to modify the routes of this template application to have a simple health check endpoint like `/healthz`, which responds with HTTP 200. This is usually a requirement with load balancing services like Azure Front Door to distribute our app globally.

Could you provide an easy way to do this or even include it into your templates?

I would imagine this to be especially necessary when using Shopify App Bridge, where the app can handle calls by the store front.

Accepted Solution (1)

Vellir
Shopify Partner
146 32 40

This is an accepted solution.

Hi Aviita,

 

It should be as simple as adding this to server/index.js:

  app.get('/healthz', (req, res) => {
    res.status(200).send();
  });


Have a look at the expressJS documentation for more examples of how to create routes.

- Looking for a Wishlist? Try First Wish

- Shopify Merchants, manage your new arrivals with Newr

- Shopify Developers, if you're looking into selling your app to focus on other projects, drop me a line.

View solution in original post

Replies 4 (4)

Vellir
Shopify Partner
146 32 40

This is an accepted solution.

Hi Aviita,

 

It should be as simple as adding this to server/index.js:

  app.get('/healthz', (req, res) => {
    res.status(200).send();
  });


Have a look at the expressJS documentation for more examples of how to create routes.

- Looking for a Wishlist? Try First Wish

- Shopify Merchants, manage your new arrivals with Newr

- Shopify Developers, if you're looking into selling your app to focus on other projects, drop me a line.
aviita
Shopify Partner
7 0 0

Could have put in more details. Been trying this and it gets hijacked by other middleware:

  app.use("/api/healthz", (req, res, next) => {
    console.log(`Health check endpoint called`);
    res.status(200).send({ success: true });
  });


And same with that `/healthz`, and this is with plain App I just created to ensure our app that has seen some development (different message though than with `/api/healthz`.

aviita_0-1669968333697.png


Placed it just above the `/*`:

  app.use("/healthz", (req, res, next) => {
    console.log(`Health check endpoint called`);
    res.status(200).send({ success: true });
  });

  app.use("/*", async (req, res, next) => {

 

aviita
Shopify Partner
7 0 0

Ha, it actually does work when deployed to server. Just some problem with my localhost setup. Thanks for inserting some more hope in me. 🙂

aviita_0-1669973643897.png

 

aviita
Shopify Partner
7 0 0

And additionally see that I slipped into registering my endpoint as middleware instead of get endpoint, but  that was my attempt to ensure it would get precedence over other middleware.

But I verified now that on server both solutions work.