github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/examples/nodejs/express-ts/index.ts (about)

     1  /* eslint-disable */
     2  import express from 'express';
     3  import morgan from 'morgan';
     4  
     5  import Pyroscope from '@pyroscope/nodejs';
     6  
     7  const port = process.env['PORT'] || 3000;
     8  
     9  const region = process.env['REGION'] || 'default';
    10  
    11  const app = express();
    12  app.use(morgan('dev'));
    13  
    14  app.get('/', (req, res) => {
    15    res.send('Available routes are: /bike, /car, /scooter');
    16  });
    17  
    18  const genericSearchHandler = (p: number) => (req: any, res: any) => {
    19    const time = +new Date() + p * 1000;
    20    let i = 0;
    21    while (+new Date() < time) {
    22      i += Math.random();
    23    }
    24    res.send('Vehicle found');
    25  };
    26  
    27  app.get('/bike', function bikeSearchHandler(req, res) {
    28    return genericSearchHandler(0.2)(req, res);
    29  });
    30  app.get('/car', function carSearchHandler(req, res) {
    31    return genericSearchHandler(1)(req, res);
    32  });
    33  app.get('/scooter', function scooterSearchHandler(req, res) {
    34    return genericSearchHandler(0.5)(req, res);
    35  });
    36  
    37  Pyroscope.init({
    38    appName: 'nodejs',
    39    serverAddress: 'http://pyroscope:4040',
    40    sourceMapPath: ['.'],
    41  });
    42  Pyroscope.startHeapProfiling();
    43  Pyroscope.startCpuProfiling();
    44  
    45  app.listen(port, () => {
    46    console.log(
    47      `Server has started on port ${port}, use http://localhost:${port}`
    48    );
    49  });