nextjs-course-code-08-prj-api-routes-starting-project

Screenshot 2024-03-14 at 10.10.13 PM.png

237. Adding a Newsletter Route

  function registrationHandler(event) {
    event.preventDefault();

    const enteredEmail = emailInputRef.current.value;

    **fetch("/api/newsletter", {
      method: "POST",
      body: JSON.stringify({
        email: enteredEmail,
      }),
      headers: {
        "Content-Type": "application/json",
      },
    })
      .then((res) => res.json())
      .then((data) => console.log(data));
  }**
// pages/api/newsletter.js

function handler(req, res) {
  if (req.method === "POST") {
    const userEmail = req.body.email;

    if (!userEmail || !userEmail.includes("@")) {
      res.status(422).json({
        message: "Invalid email address.",
      });
      return;
    }

    console.log(userEmail);

    res.status(201).json({
      message: "Signed up!",
    });
  }
}

export default handler;

238. Adding Comments API Routes

240. Setting Up A MongoDB Database

241. Running MongoDB Queries From Inside API Routes

Screenshot 2024-03-15 at 4.31.05 AM.png

application code

mongodb+srv://<username>:<password>@cluster0.tblijqc.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0

how to set up