0.1.0 • Published 1 year ago

@fetchlytics/express-middleware v0.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

FetchLytics Middleware

FetchLytics is a SaaS that provides analytics and insights for network requests. This library allows you to track backend request analytics in your Express applications with minimal setup.

Installation

Install the library via npm:

npm install @fetchlytics/express-middleware

Usage

The trackRequestAnalytics function captures request analytics for each request in your Express app, sending data such as request duration, status code, and method to the FetchLytics API. The middleware dynamically loads node-fetch to send data only when necessary.

Import the middleware

Import trackRequestAnalytics and the necessary types:

const {NextFunction, Request, Response} = require('express'); // Import Express types for type safety
const {trackRequestAnalytics} = require('@fetchlytics/express-middleware'); // Import the middleware
// OR
import {NextFunction, Request, Response} from 'express'; // Import Express types for type safety
import {trackRequestAnalytics, FetchAnalyticsOptions} from '@fetchlytics/express-middleware'; // Import the middleware

Initialize the middleware

To initialize the middleware, pass in an object with your FetchLytics API key and app name. The order of the middleware is important, the tracking starts when this middleware is called, so it should be placed before any other middleware that may affect the request. But it should be placed after the CORS middleware - see the next section for more details.

const options: FetchAnalyticsOptions = {
	apiKey: 'your-fetchlytics-api-key', // Replace with your API key
	appName: 'your-app-name', // Replace with your app name
};

app.use(trackRequestAnalytics(options)); // Add the middleware to you express app

Configuring CORS

To track client requests with FetchLytics, ensure that the fetchlytics-id header is allowed in your CORS configuration. For example, using the cors middleware:

const cors = require('cors');
// OR
import cors from 'cors';

app.use(cors({
  ... // Your existing CORS configuration
  allowedHeaders: ['fetchlytics-id'], // Include 'fetchlytics-id' in allowed headers
}));

Example

Here's a complete example of integrating FetchLytics into an Express app:

import express, {NextFunction, Request, Response} from 'express';
import cors from 'cors';
import {trackRequestAnalytics, FetchAnalyticsOptions} from '@fetchlytics/express-middleware';

const app = express();

const options: FetchAnalyticsOptions = {
	apiKey: 'your-fetchlytics-api-key', // Replace with your actual API key
	appName: 'my-app',
};

// Configure CORS to allow 'fetchlytics-id' header
app.use(
	cors({
		origin: 'your-allowed-origin',
		allowedHeaders: ['fetchlytics-id', 'Content-Type'],
	})
);

app.use(trackRequestAnalytics(options));

app.get('/', (req: Request, res: Response) => {
	res.send('Hello, FetchLytics!');
});

app.listen(3000, () => {
	console.log('Server is running on port 3000');
});

How it works

The trackRequestAnalytics middleware intercepts requests, recording informations such as:

  • fetchlytics-id: a unique identifier for the request, passed in the fetchlytics-id header
  • url: full URL of the request
  • method: HTTP method of the request
  • status: HTTP status code of the response
  • duration: duration of the request in milliseconds
  • timestamp: timestamp of the request

This data is sent to Fetchlytics for processing and can be viewed in the FetchLytics dashboard.

Error handling

In case of errors while sending analytics data, the library logs them to the console without interrupting the request flow.

License

This library is licensed under the FetchLytics License. See the LICENSE file for more details.

Support

For any questions or issues, please contact support@fetchlytics.dev. We're happy to help!

0.1.0

1 year ago