Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Node.JS Nextjs on Vercel, having issues starting off

suss

New Coder
Hi, I hope someone can help, but this question may only make sense to those familiar with Nextjs on Vercel host.
Ive posted this in the Node forum as Nextjs runs on top of Node so it was closest category

I'm not a React expert, and have really struggled to get much running in Nextjs, even though Vercel does all the hosting so I dont need to install the frameworks.

Ive started with the simplest stack example, just index html file and a function in the pages/api directory where 'backend' api code is supposed to go.
The public/index.html and pages/api/myapi.js shown below. It doesn't work. Is there anything obvious error?

JavaScript:
<!DOCTYPE html>
<html>
 <body>
  <script>
 
  function myPost(){
   var postData = async () => {
     const data = {title: "one",post: "two"};
     const resp = await fetch("../pages/api/myapi.js", {
       method: "POST",
       body: JSON.stringify(data),
     });
 
   return resp.json();
  }
 
  myPost();
  postData().then((d) => { alert(d.message) });

  </script>
 </body>
</html>

And pages/api/myapi.js
JavaScript:
export default function handler(req, res) {
  const requestMethod = req.method;
  const body = JSON.parse(req.body);
  switch (requestMethod) {
    case 'POST':
      res.status(200).json({ message: `You submitted the following data: ${body}` })
 
    // handle other HTTP methods
    default:
      res.status(200).json({ message: 'Welcome to myAPI!'})
  }
}
 
Last edited:
correction in index.html
Code:
<!DOCTYPE html>
<html>
 <body>
  <script>
 
  function myPost(){
   var postData = async () => {
     const data = {title: "one",post: "two"};
     const resp = await fetch("../pages/api/myapi.js", {
        method: "POST",
        body: JSON.stringify(data),
       };
     }
 
   return resp.json();
  }
 
  myPost();
  postData().then((d) => { alert(d.message) });

  </script>
 </body>
</html>
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom