Next.js Discord

Discord Forum

Does my connection to the database look correct? Api routes am I using them correctly?

Unanswered
Николя posted this in #help-forum
Open in Discord
I'm writing a website for watching anime. This is my first time working with api routes, do I have the correct structure? I recently had an error that too many connections were being created, I decided to make a pool, it didn’t seem like a good solution, did I do it right? Do I understand correctly that each page needs its own api routes?
My pool
And one example api route
import { NextResponse } from "next/server";
import pool from "@/db";

export async function GET() {
    try {
        const results = await new Promise((resolve, reject) => {
            pool.query('SELECT * FROM series', (err, results) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(results)
                }
            });
        });
        console.log(results);
        return NextResponse.json(results);
    } catch (error) {
        return NextResponse.json(
            { message: error },
            {
                status: 500
            }
        );
    }
}

1 Reply

:thinq: