Next.js Discord

Discord Forum

Nextjs api not working

Answered
Silver cyprinid posted this in #help-forum
Open in Discord
Silver cyprinidOP
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
Answered by Ray
src/app/api/githubPull/route.ts
View full answer

122 Replies

Silver cyprinidOP
im sure that the payload url is set correctly in github
Answer
Silver cyprinidOP
yes
export async function POST() {}
Silver cyprinidOP
yes
well I used export default does it matter?
Silver cyprinidOP
lemme check again I really forgot
oh its export default
lemme try
@Ray I changed it and its the same :p_amsyConfused:
you got a 404 or what?
try curl -X POST https://your-url/api/githubPull
Silver cyprinidOP
ok
@Ray you got a 404 or what? try `curl -X POST https://your-url/api/githubPull`
Silver cyprinidOP
curl is having some problems with me so im gonna try node fetch one sec
Silver cyprinidOP
@Ray 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
what is this
Silver cyprinidOP
idk
I send a fetch request and it returned it
invalid json response body
look like your endpoint return invalid json response body
@Ray invalid json response body
Silver cyprinidOP
lemme check
@Ray I removed everything that returns a json response body
it literally just returns the status now
:amcrying:
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 @Ray
Silver cyprinidOP
which part?
:p_amsyConfused:
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 });
  }
}
Silver cyprinidOP
alright thanks
what do you mean same thing
you should check the error
@Ray what do you mean same thing
Silver cyprinidOP
same error
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
Silver cyprinidOP
I just wanna see what it returns, it doesnt have to be json
lemme try
I don't know what the error is
Silver cyprinidOP
yes
wait its because I used data.json() in node fetch
:face_plam:
now it doesnt return anything
Silver cyprinidOP
then it wouldnt say compiling / not-found with github thats what makes me confused
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
@Ray 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
Silver cyprinidOP
in that case it would say compiling / not / not-found
:d_ayaku:
if you see compiling /not-found which mean you are accessing a route which doesn't exist
but why would it say that when im accessing a route that exists
what do you see then
if you thing the route exist
Silver cyprinidOP
well if I open that api route by myself it says compiling /api/githubPull
but with github it says / not-found
so the url on github is wrong then
Silver cyprinidOP
it shouldnt be because im sure
maybe because Im using my host's localhost option?
then why not-found?
@Silver cyprinid maybe because Im using my host's localhost option?
Silver cyprinidOP
even tho its accesible everywhere
@Ray and you definitely can access it
Silver cyprinidOP
well yeah
@Silver cyprinid maybe because Im using my host's localhost option?
how can github access your localhost?
do you think if I go to http://localhost:3000/api/githubPull and It will open the route on your pc?
its a host
I dont even know how can a localhost in the eu be accessed in the middle east
i don't understand what you are trying to do so good luck with that
Silver cyprinidOP
it eventually returned 404 not found in node fetch @Ray
:p_amsyConfused:
@Ray i don't understand what you are trying to do so good luck with that
Silver cyprinidOP
I used the global network option now
What is your latest code for now?
@Silver cyprinid <a:Amsy:1202442861761667204>
Can you remove the exec part and console.log the route
@aardani Can you remove the exec part and console.log the route
Silver cyprinidOP
what exactly is route? :d_ayaku:
pwd?
Silver cyprinidOP
before any other code right? one sec
Yeah
Send the full path of your POST file
And also send how you call the POST route
Silver cyprinidOP
ima send you the code and the file structure in my code editor one sec
Silver cyprinidOP
@aardani
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
Silver cyprinidOP
okay
@Silver cyprinid Click to see attachment
why is it still script.ts
Ray told you to rename to route.ts
Silver cyprinidOP
when?
:p_amsyConfused:
(╯°□°)╯︵ ┻━┻
Silver cyprinidOP
:d___: :d_aaawhy:
sorry~
yeah lmk if it console log the POST route
Silver cyprinidOP
now it returns a server error which means its not 404 not found
Nice
its working now
Silver cyprinidOP
[object object] is not a valid json parse
I think it has to do with the ref part
If you have any problem feel free to open a new post forum. The original query has been answered :meow_coffee:
Silver cyprinidOP
alright
thanks to all three of you and I apologize to @Ray I thought its not important to have it named route.ts
:nm:
@Silver cyprinid thanks to all three of you and I apologize to <@743561772069421169> I thought its not important to have it named `route.ts`
The reason im doing is that so that people wont need to read the entire chat just to get the whole problem.
I advise providing a description of what the issue is, what you have done, and what you expect it to do.
Additional points if you can give relevant code
I hope you understand, i dont intent on cutting you off
Silver cyprinidOP
its alright
I apologize once again