#Nextjs api not working

124 messages · Page 1 of 1 (latest)

barren furnace
#

I have this project structure here:

- src
|
| - app 
  |
  | - api
    |
    | - githubPull
       | - script.ts

basically I want github to send a POST request to /api/githubPull then I can pull the changes from github
but everytime I try to commit a change nextjs logs:
compiling / not-found
compiled / not-found in xx seconds
and nothing is pulled, not even a single script works

raw plazaBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

barren furnace
#

im sure that the payload url is set correctly in github

inner jetty
barren furnace
#

yes

inner jetty
#
export async function POST() {}
barren furnace
#

yes

#

well I used export default does it matter?

barren furnace
#

lemme check again I really forgot

#

oh its export default

#

lemme try

#

@inner jetty I changed it and its the same p_amsyConfused

inner jetty
#

you got a 404 or what?
try curl -X POST https://your-url/api/githubPull

barren furnace
#

ok

barren furnace
#

@inner jetty it returned this

json() {
        var _this2 = this;

        return consumeBody.call(this).then(function (buffer) {
            try {
                return JSON.parse(buffer.toString());
            } catch (err) {
                return Body.Promise.reject(new FetchError(`invalid json response body at ${_this2.url} reason: ${err.message}`, 'invalid-json'));
            }
        });
    }```
#

wait invalid json

#

but nothing is wrong in my request

inner jetty
#

what is this

barren furnace
#

idk

inner jetty
#

invalid json response body

barren furnace
#

I send a fetch request and it returned it

inner jetty
#

look like your endpoint return invalid json response body

barren furnace
#

@inner jetty I removed everything that returns a json response body

#

it literally just returns the status now

#

and still

#

same error

#
import type { NextApiRequest, NextApiResponse } from 'next';
import { execSync } from 'child_process';

export async function POST(req: NextApiRequest, res: NextApiResponse) {

  try {
    const { ref }: { ref: string } = await req.body as { ref: string };

    await execSync(`git pull origin ${ref}`);

    res.status(200)
  } catch (error) {
    console.error(error);
    res.status(500)
  }
}
#

here is the code @inner jetty

barren furnace
#

which part?

inner jetty
#
export async function POST(req: Request) {
  try {
    const { ref }: { ref: string } = (await req.json()) as { ref: string };

    await new Promise((resolve, reject) => {
      exec(`git pull origin ${ref}`, (err, stdout) => {
        if (err) {
          reject(err);
        } else {
          resolve(stdout);
        }
      });
    });

    return new Response();
  } catch (error) {
    console.error(error);
    return new Response("", { status: 500 });
  }
}
barren furnace
#

alright thanks

barren furnace
#

whats wrong now p_amsyConfused

inner jetty
#

what do you mean same thing

#

you should check the error

barren furnace
inner jetty
#
export async function POST(req: Request) {
  try {
    const { ref }: { ref: string } = (await req.json()) as { ref: string };

    await new Promise((resolve, reject) => {
      exec(`git pull origin ${ref}`, (err, stdout) => {
        if (err) {
          reject(err);
        } else {
          resolve(stdout);
        }
      });
    });

    return Response.json();
  } catch (error) {
    console.error(error);
    return Response.json({ error }, { status: 500 });
  }
}
#

return an json if you are expecting json in response

barren furnace
#

I just wanna see what it returns, it doesnt have to be json

#

lemme try

inner jetty
#

I don't know what the error is

inner jetty
barren furnace
#

yes

#

wait its because I used data.json() in node fetch

#

now it doesnt return anything

inner jetty
barren furnace
#

then it wouldnt say compiling / not-found with github thats what makes me confused

inner jetty
#

I don't know what you see but when you running with next dev and trying to access the site first time it will say that

barren furnace
inner jetty
#

if you see compiling /not-found which mean you are accessing a route which doesn't exist

barren furnace
#

but why would it say that when im accessing a route that exists

inner jetty
#

what do you see then

#

if you thing the route exist

barren furnace
#

well if I open that api route by myself it says compiling /api/githubPull

#

but with github it says / not-found

inner jetty
#

so the url on github is wrong then

barren furnace
#

it shouldnt be because im sure

#

maybe because Im using my host's localhost option?

inner jetty
#

then why not-found?

barren furnace
inner jetty
barren furnace
inner jetty
barren furnace
#

its a host

#

I dont even know how can a localhost in the eu be accessed in the middle east

inner jetty
#

i don't understand what you are trying to do so good luck with that

barren furnace
#

it eventually returned 404 not found in node fetch @inner jetty

barren furnace
chrome kraken
#

What is your latest code for now?

chrome kraken
barren furnace
#

pwd?

chrome kraken
barren furnace
#

before any other code right? one sec

chrome kraken
#

Yeah

#

Send the full path of your POST file

#

And also send how you call the POST route

barren furnace
#

ima send you the code and the file structure in my code editor one sec

#

@chrome kraken

#

I meant and

#

uhh I dont know why it sent the code snap as a file

#

2 bytes only?

#

I think its corrupt

#

one sec

fringe summit
barren furnace
#

okay

chrome kraken
#

Ray told you to rename to route.ts

barren furnace
#

when?

chrome kraken
#

(╯°□°)╯︵ ┻━┻

barren furnace
#

sorry~

chrome kraken
#

yeah lmk if it console log the POST route

barren furnace
#

now it returns a server error which means its not 404 not found

chrome kraken
#

Nice

#

its working now

barren furnace
#

[object object] is not a valid json parse

#

I think it has to do with the ref part

raw plazaBOT
#
✅ Success!

This question has been marked as answered! If you have any other questions, feel free to create another post

Jump to answer
chrome kraken
#

If you have any problem feel free to open a new post forum. The original query has been answered meow_coffee

barren furnace
#

alright

#

thanks to all three of you and I apologize to @inner jetty I thought its not important to have it named route.ts

chrome kraken
barren furnace
#

its alright