Tuomo Kankaanpää
⬛Next.js
1y ago
How to save data to a database in Next.js?
When ever we are working on the server, we can directly access a database. In Next.js we have couple of ways to do that.
Let's dive in and see what those are:
1/4
Tuomo Kankaanpää
1y ago
API Routes
The OG way to create endpoints in Next.js. Api routes are used in the Pages-router and they create an endpoint that we can send data to. They are run on the server so we can access database directly from an api route.
2/4
Tuomo Kankaanpää
1y ago
Route Handlers
A bit newer member to the party. Route handlers are the App-router equivalent for API Routes. They too are run on the server and thus we can access database directly from them. They also create an api endpoint we can send data to.
3/4
Tuomo Kankaanpää
1y ago
Server Actions
Our cutting edge option! Server actions can be confusing at first but once you get few miles under your belt using them, it starts to make sense. They can be used directly in both server & client components without needing to use API Routes or Route Handlers.
4/4