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.
Search API endpoint - Next.js Tutorial
From the course: Next.js 14 from Scratch: Build a Real-World Project Using Next.js 14 and MongoDB
Search API endpoint
All right, guys, so now we want to create our endpoint that we want to hit to get our search results. So we're going to close up the property search form. And let's open up API. And where this is going to be is API slash properties and then slash search. So we have to create a folder in here called search. Make sure you're in your API properties, not just properties. And then in search, we'll create our route.js file. So in here, we're going to use the database. So we want to import ConnectDB. We want to use the property model. So let's bring that in. And then this route is going to be get and then slash API slash properties slash search. So let's say export const get equals async and pass in our request. Then let's open up try catch. And we want to connect to our database. Now we want to get the search query from the URL, the query params. So we can get that with request. Actually, I'll show you. We can destructure and get search params. We can get that from new URL and then pass in…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.