1.0.3 • Published 1 year ago
express-endpoints-monitor v1.0.3
Registering and exporting endpoints from within your code
- Install and require in the
express-endpoints-monitor
in your project directory:$ npm install express-endpoints-monitor -–save
In yourserver.js
file:const { gatherMetrics, registerEndpoint, exportEndpoints, exportAllEndpoints, startMetricsServer } = require(“express-endpoints-monitor”);
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 );
Start your server the normal way. You should see in the terminal:
$ Metrics server started on port 9991
- The metrics server provides three endpoints:
GET http://localhost:9991/endpoints
GET http://localhost:9991/metrics
DELETE http://localhost:9991/metrics
- 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", } ]
- 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 }
- The
DELETE http://localhost:9991/metrics
responds with the same array but clears the logs from memory