DEV Community

Vigilmon
Vigilmon

Posted on

How to Monitor Your Bun.js Application with Vigilmon

How to Monitor Your Bun.js Application with Vigilmon

Bun is a fast all-in-one JavaScript runtime — bundler, transpiler, package manager, and test runner. As more teams adopt Bun for production services, monitoring becomes essential.

This guide covers how to add health check monitoring to your Bun HTTP server and connect it to Vigilmon.

Creating a Health Endpoint in Bun

Bun has a built-in HTTP server (Bun.serve) that's incredibly fast:

const server = Bun.serve({
  port: 3000,
  async fetch(req) {
    const url = new URL(req.url);

    if (url.pathname === '/health') {
      return new Response(
        JSON.stringify({ status: 'ok', runtime: 'bun', version: Bun.version }),
        {
          status: 200,
          headers: { 'Content-Type': 'application/json' }
        }
      );
    }

    // Other routes...
    return new Response('Not Found', { status: 404 });
  }
});

console.log(`Server running on http://localhost:${server.port}`);
Enter fullscreen mode Exit fullscreen mode

With Database Check (using Bun's SQLite or postgres)

import { Database } from 'bun:sqlite';

const db = new Database('myapp.db');

const server = Bun.serve({
  port: 3000,
  async fetch(req) {
    const url = new URL(req.url);

    if (url.pathname === '/health') {
      try {
        db.query('SELECT 1').run();
        return Response.json({ status: 'ok', db: 'connected' });
      } catch (e) {
        return Response.json(
          { status: 'error', db: 'disconnected' },
          { status: 503 }
        );
      }
    }

    return new Response('Not Found', { status: 404 });
  }
});
Enter fullscreen mode Exit fullscreen mode

Using Hono with Bun

Hono is the most popular routing framework for Bun:

import { Hono } from 'hono';

const app = new Hono();

app.get('/health', (c) => {
  return c.json({ status: 'ok', runtime: 'bun' });
});

app.get('/health/db', async (c) => {
  try {
    // Check your database
    await db.execute('SELECT 1');
    return c.json({ status: 'ok', db: 'connected' });
  } catch {
    return c.json({ status: 'error', db: 'disconnected' }, 503);
  }
});

export default app; // Bun will use this automatically
Enter fullscreen mode Exit fullscreen mode

Adding Vigilmon Monitoring

  1. Create a free account at vigilmon.online
  2. Click Add Monitor
  3. Enter your Bun app's health URL: https://your-bun-app.com/health
  4. Set check interval: 1 minute
  5. Enable multi-region checks — verify your app is up globally
  6. Add your alert contact (email, Slack, webhook)

Deploying Bun Apps for Monitoring

Bun runs natively on Linux. Common deployment patterns:

# systemd service for Bun app
[Unit]
Description=My Bun App
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/var/www/my-bun-app
ExecStart=/usr/local/bin/bun run start
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Set Restart=always so systemd restarts your Bun app if it crashes, and let Vigilmon alert you if a restart loop is occurring (response time spikes + periodic downtime).

Monitoring Bun in Docker

FROM oven/bun:1 AS base
WORKDIR /app

COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile

COPY . .

EXPOSE 3000

HEALTHCHECK --interval=30s --timeout=10s \n  CMD curl -f http://localhost:3000/health || exit 1

CMD ["bun", "run", "start"]
Enter fullscreen mode Exit fullscreen mode

Docker's HEALTHCHECK works alongside Vigilmon — Docker restarts the container internally, Vigilmon tells you it happened.

Conclusion

Bun's speed and simplicity make it great for production services. Pair it with Vigilmon for external uptime monitoring and you'll catch issues before your users do.

Start monitoring your Bun app at vigilmon.online.

Top comments (0)