Express

Express

Scout supports Express 4.x+.

1. Install the @scout_apm/scout-apm package:

yarn add @scout_apm/scout-apm

2. Add to Express Middleware:

// Require scout-apm first, before other requires
const scout = require("@scout_apm/scout-apm");
const express = require("express");

// The "main" function
async function start() {
  // Trigger the download and installation of the core-agent
  await scout.install({
    allowShutdown: true, // allow shutting down spawned scout-agent processes from this program
    monitor: true, // enable monitoring
    name: "",
    key: "",
  });

  // Initialize your express application
  const app = express();

  // Enable the app-wide scout middleware
  app.use(scout.expressMiddleware());

  // Add other middleware and routes
  // app.use( ... )
  // app.get( ... )

  // Start express
  app.start();
}

// If this script is executed directly, run the start function
if (require.main === module) { start(); } 

3. Configure Scout via ENV variables:

export SCOUT_MONITOR=true

export SCOUT_KEY="[AVAILABLE IN THE SCOUT UI]"

export SCOUT_NAME="A FRIENDLY NAME FOR YOUR APP"

NOTE Pass configuration to scout.install and if a scout agent instance does not exist already one will be created for you on the fly. After awaiting or .thening the Promise returned by scout.install, you can be sure that the scout agent is available and enable the middleware by calling app.use(scout.expressMiddleware()). If you do not call scout.install({ ... }) and wait for setup to complete, the first inbound request will start the setup and eventually requests will be recorded (setup will not block requests, and recording will start when the agent has been set up).

If you’ve installed Scout via the Heroku Addon, the provisioning process automatically sets SCOUT_MONITOR and SCOUT_KEY via config vars. Only SCOUT_NAME is required.

4. Deploy.

It takes just a few minutes for your data to first appear within the Scout UI.