#404

1 messages · Page 1 of 1 (latest)

sage anchorBOT
#

Hi @iron dagger. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the provided knowledge sources, it seems like you're encountering a 404 error when making a request to your API endpoint (/api/books/${encodedQuote}). This error typically means that the server can't find the requested resource.

Here are a few things you could check:

  1. Ensure that the API endpoint exists and is correctly spelled in your server. In your case, it should be /api/books/{quote} where {quote} is a URL-encoded string.

  2. Check if the server is running and accessible from your Next.js application.

  3. If you're using dynamic routing in your API routes, make sure that the file is correctly named and located in the pages/api directory.

  4. Check the network tab in your browser's developer tools to see the exact request being made and the response received. This can give you more information about what might be going wrong.

Unfortunately, without more specific information or code related to your API setup, it's hard to provide a more precise solution. If you're still having trouble, you might find it helpful to review the Next.js documentation on API routes (https://nextjs.org/docs/api-routes/introduction).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@iron dagger kapa.ai is still learning and improving, please let us know how it did by reacting below

iron dagger
# sage anchor Hi <@1023637894491033711>. I'm kapa.ai, your friendly support bot. I will try to...

@sage anchor

// app/api/books/[quote].js
import axios from 'axios';
import cheerio from 'cheerio';

export default async function GET({ params }) {
  const { quote } = params;

  // The rest of the code remains largely unchanged
  const formattedQuote = encodeURIComponent(quote);
  try {
    const searchUrl = `https://www.google.com/search?tbm=bks&q="${formattedQuote}"`;
    const { data } = await axios.get(searchUrl, {
      headers: {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
      },
    });

    const $ = cheerio.load(data);
    const titleAndPage = $('.LC20lb.MBeuO.DKV0Md').first().text();
    const authorAndYear = $('.N96wpd').first().text();

    return new Response(JSON.stringify({ titleAndPage, authorAndYear }), {
      status: 200,
      headers: {
        "Content-Type": "application/json",
      },
    });
  } catch (error) {
    return new Response(JSON.stringify({ error: error.toString() }), {
      status: 500,
      headers: {
        "Content-Type": "application/json",
      },
    });
  }
}

app/api/books/[quote].js
current js

sage anchorBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The code you've provided is a Next.js API route that fetches book details based on a quote. It uses the axios library to make a request to Google Books search, and the cheerio library to parse the HTML response and extract the book's title, page, author, and year.

Here's a brief explanation of how it works:

  1. The GET function is exported as the default function for this API route. It's an asynchronous function that takes a params object as an argument. The params object contains route parameters, in this case, the quote.

  2. The quote is URL-encoded and used to construct a Google Books search URL.

  3. An HTTP GET request is made to the search URL using axios. A custom User-Agent header is set for the request.

  4. If the request is successful, the HTML data from the response is loaded into cheerio, a fast, flexible, and lean implementation of core jQuery designed specifically for the server.

  5. The book's title and page, and the author and year are extracted from the HTML using cheerio selectors and returned in the response as a JSON object.

  6. If an error occurs during the request, a response with a status of 500 is returned, along with a JSON object containing the error message.

Please note that this code is dependent on the structure of the Google Books search results page. If Google changes the structure of their pages, the selectors used to extract the book details may no longer work.

Also, be aware that web scraping Google search results is against Google's Terms of Service. You might want to consider using the Google Books API (https://developers.google.com/books) instead, which provides a more reliable and legal way to search for books.

#

For more information on how to work with API routes in Next.js, you can refer to the Next.js documentation (https://nextjs.org/docs/api-routes/introduction).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: