npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gpt-flow/express

v1.0.3

Published

Express middleware for logging errors to GPT-Flow

Downloads

543

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

  1. 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');
    });
  2. 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
  1. Go to http://localhost:3000/ in your browser or use a tool like Postman or curl to make requests.
  2. To simulate an error, visit http://localhost:3000/error, and GPT-Flow should capture and log the error.