@gpt-flow/express
v1.0.3
Published
Express middleware for logging errors to GPT-Flow
Downloads
8
Readme
@gpt-flow/express
A middleware for Express.js applications to capture, log, and send error reports and request data to the GPT-Flow API for analysis.
Installation
Install the package via npm:
npm install @gpt-flow/express
Basic Setup
Step 1: Environment Variables Setup
You need to provide your projectId
, publicKey
, and projectUrl
for GPT-Flow to function properly. Add these variables to a .env
file at the root of your project:
PROJECT_ID=your_project_id
PUBLIC_KEY=your_public_key
PROJECT_URL=https://gpt-flow-llm-api.onrender.com
Step 2: Configure GPT-Flow Programmatically
If you don’t want to use a .env
file, you can configure GPT-Flow directly in your application code:
const gptFlow = require('@gpt-flow/express');
gptFlow.configure({
projectId: 'your_project_id', // Replace with your actual project ID
publicKey: 'your_public_key', // Replace with your actual public key
projectUrl: 'https://gpt-flow-llm-api.onrender.com' // Your GPT-Flow URL
});
Step 3: Using the Middleware
Add the GPT-Flow middleware to your Express app:
const express = require('express'); const { errorHandlingMiddleware } = require('@gpt-flow/express'); const app = express(); // Use GPT-Flow middleware to handle errors app.use(errorHandlingMiddleware()); // Example route app.get('/', (req, res) => { res.send('Hello, GPT-Flow!'); }); // Error route to simulate an error app.get('/error', (req, res) => { throw new Error('This is a simulated error.'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Customizing Error Responses: If you want to customize how errors are handled or how the user is notified, you can pass an options object when using
errorHandlingMiddleware
:app.use(errorHandlingMiddleware({ customResponse: 'Something went wrong! Our team is looking into it.', renderErrorPage: false // Set to true if you want to render a custom error page }));
Step 4: Additional Features (Optional)
Request Logging
To log every request made to the server:
const { requestLoggingMiddleware } = require('@gpt-flow/express');
// Use request logging middleware
app.use(requestLoggingMiddleware);
Request Validation (Using Joi)
To validate incoming request payloads, you can integrate Joi
:
const Joi = require('joi');
const { requestValidationMiddleware } = require('@gpt-flow/express');
// Define a schema
const schema = Joi.object({
name: Joi.string().min(3).required(),
});
// Use validation middleware
app.post('/data', requestValidationMiddleware(schema), (req, res) => {
res.send('Data received successfully');
});
Handling Asynchronous Errors
For handling errors in async routes, use the asyncErrorHandler
function:
const { asyncErrorHandler } = require('@gpt-flow/express');
// Example async route with error handling
app.get('/async-error', asyncErrorHandler(async (req, res, next) => {
const result = await someAsyncOperation(); // Simulating async operation
res.send(result);
}));
Step 5: Testing the Middleware
After setting everything up, you can test it by running your Express server:
node index.js
- Go to
http://localhost:3000/
in your browser or use a tool like Postman or curl to make requests. - To simulate an error, visit
http://localhost:3000/error
, and GPT-Flow should capture and log the error.