Next.js Discord

Discord Forum

Post method returing 405 in prod

Unanswered
ItetsuLaTable posted this in #help-forum
Open in Discord
Hello everyone, I have built an app which generate a lighthouse report by a given json.
It was working well since few months but now my api return me a 405 and I dunno why...

8 Replies

Here is my code
 const handler = async () => {
        if(fileData === "") {
            alert("Please select a file");
            return;
        }
        if(output === "") {
            alert("Please enter a output file name");
            return;
        }
        setLoading(true);
        fetch('/api/lighthouse', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                "input": JSON.parse(fileData),
            })
        }).then( async (res) => {
            if(res.status === 200) {
                const response = await res.json() ;
                createFileandDownload(response);
            }

        }).catch((error) => {
            setError(error);
        }).finally(() => {
            setLoading(false);
        }) ;
    }
It calls my own api (I have a lighthouse file in api folder)
and this is my api code
import lighthouse from "lighthouse";
import puppeteer from 'puppeteer';


export const launchChromeAndRunLighthouse = async (url) => {

   const browser = await puppeteer.launch({
       headless: "new",
       defaultViewport: null,
       ignoreDefaultArgs: ['--enable-automation'],
   }) ;

   const page = await browser.newPage();
   const {lhr} = await lighthouse(url, {
       skipAudits: ["pwa"],
       preset: "desktop",
       quiet: true,
       chromeFlags: ["--headless", "--disable-gpu", "--no-sandbox", "--disable-dev-shm-usage"],
       disableFullPageScreenshot: true,
       output: "json",
   }, undefined, page);
   await browser.close();

   return lhr ;

}


const handler = async (req,res) => {

    const input = req.body?.input ;
    const data = [] ;

    try {

        const urls = input ;

        for (const url of urls) {
            data.push({
                url: url ,
                data : await launchChromeAndRunLighthouse(url)
            });
        }
        return res.status(200).json(data) ;

    } catch (error) {
        return res.status(500).json({error}) ;
    }

}

export default handler ;
when running in local it works fine
but i've upload it through vercel and it returns me 405