express-endpoints-monitor

1.0.3 • Public • Published

Registering and exporting endpoints from within your code

  1. Install and require in the express-endpoints-monitor in your project directory: $ npm install express-endpoints-monitor -–save

    In your server.js file:

    const {
      gatherMetrics,
      registerEndpoint,
      exportEndpoints,
      exportAllEndpoints,
      startMetricsServer } = require(“express-endpoints-monitor”);
    
  2. The module provides several functions to register one, several, or all endpoints of your express application and gather metrics:

    app.use('/',
      gatherMetrics
    );
    
    app.get(‘/some/arbitrary/route’,
    	registerEndpoint,
    	someOtherMiddleware,
    	...
    	(req, res) => return status(200).send(‘hello world!’);
    );
    
    app.post(‘/another/route’,
    	theOrderDoesntMatter,
    	registerEndpoint,
    	...
    	(req, res) => return status(204));
    );
    
    app.listen(APP_SERVER_PORT,
    	exportEndpoints(); // This will export only the selected endpoints with the registerEndpoint middleware
    	startMetricsServer(METRICS_SERVER_PORT); // If METRICS_SERVER_PORT is not defined, this value will default to 9991
    );
    

    OR

    app.get(‘/some/arbitrary/route’,
    	...
    	(req, res) => {...};
    );
    
    app.post(‘/another/route’,
    	...
    	(req, res) => {...});
    );
    
    app.listen(APP_SERVER_PORT,
    	exportAllEndpoints(); // This will export all endpoints
    	startMetricsServer(METRICS_SERVER_PORT); // If METRICS_SERVER_PORT is not defined, this value will default to 9991
    );
    
  3. Start your server the normal way. You should see in the terminal:

    $ Metrics server started on port 9991

  4. The metrics server provides three endpoints: GET http://localhost:9991/endpoints GET http://localhost:9991/metrics DELETE http://localhost:9991/metrics

  5. The GET http://localhost:9991/endpoints endpoint responds with an array of the exported endpoints:

    [
      {
        "path": "/some/arbitrary/route",
    	"method": "GET",
      },
      {
        "path": "/another/route",
    	"method": "POST",
      }
    ]
    
  6. The GET http://localhost:9991/metrics endpoint responds with an array of logs in the format:

    {
      date_created: , // Date
      path:  , // String
      url: , // String
      method: , // String
      status_code: , // Number
      response_time: , // Number
    }
    
  7. The DELETE http://localhost:9991/metrics responds with the same array but clears the logs from memory

Readme

Keywords

none

Package Sidebar

Install

npm i express-endpoints-monitor

Weekly Downloads

0

Version

1.0.3

License

ISC

Unpacked Size

4.94 kB

Total Files

3

Last publish

Collaborators

  • jh51