From the course: Next.js 14 from Scratch: Build a Real-World Project Using Next.js 14 and MongoDB

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Message submit and API route

Message submit and API route

In the last video, we created our message model. And we also added the state to our contact form on the property page and just created this data object and just logged it. So now we want to create the API route to submit this data to. So let's go to our app API folder. And we're going to create a new folder called Messages. And inside Messages, we'll create our route JS. And we're going to want to have our post. We're going to handle a post request to submit the message. So let's import a couple of things. We want our ConnectDB. We want the message model that we created in the last video. And then we also want the get session user from our utils file. And we're also going to add that export const dynamic and set that to force-dynamic, or else we may have issues when we deploy. So we're going to have a POST request to slash API slash messages. So I've been trying to stick with the RESTful routes throughout this project. So let's export const post, set that to async, pass in the…

Contents