How to build APIs with Next.js? ft Tim Neutkens | Prismic
Prismic
Channel
Interviewed Person
Tim Neutkens
Description
Next.js introduced a way to have APIs built into it: you can add a pages/api directory, each file there will be a request handler (not a React component :) ) based on Express. This is different from a monolithic approach because, in production, each API will be deployed as a different serverless function. This will make the bundle smaller when the code is executed and the project more distributed, more scalable, and isolated by default. More videos on Next.js: https://www.youtube.com/playlist?list=PLUVZjQltoA3yUQNXfyoGDDK4wuQdOOah5 --- As a developer, you should build websites using your favorite Jamstack framework. Prismic allows you to build website sections, that you can connect to a website builder for your client or team. They will create pages from there and you get that content back to your code through our fast API. ► [Tutorial] Build a full website with Next.js 13, Prismic, Tailwind and Typescript: https://youtu.be/nfZu56KsK_Q ► [Tutorial] Build a full website with Nuxt 3 and Prismic's new Page Builder: https://youtu.be/8GmfcbuYOWE ► [Starters] Try Slice Machine on Nuxt: https://prismic.club/nuxt-starters ► [Starters] Try Slice Machine on Next.js: https://prismic.club/nextjs-starters ► [Learn more about Slice Machine]: https://prismic.club/slice-machine --- ► Find us also on: Twitter: https://twitter.com/prismicio Instagram: https://www.instagram.com/prismicio LinkedIn: https://www.linkedin.com/company/prismic-io ► [Who are we?] : Prismic is a headless Website Builder, for Next.js and Nuxt.js developers. --- 00:00 Intro 00:33 Create your IPA with Next 03:50 Difference with the monolith approach #Prismic #Next #WebDevelopment
Tags
Transcript
- So we're here in Prismic studio, and I'm still with Tim, and we had several videos before where we talked about different things, but one of the things we talked about is the fact that you can start with Next simply and you move on to more, like you can start with just your pages like what you would do with PHP, just a file per page, and then you can add more dynamism
with dynamic rendering, and then we move to another thing is that you can write your APIs with Next. - Right, so something that we recently introduced is that you can do so you can build your whole React application but it's generally always front-end right, so you built your whole front end in React + Next for example and that covers like all the front-end needs that you have, except like generally you have
more needs than just building the front end. For example you have a checkout that has a payment API call that it needs to make, so what we recently introduced is having an API built into Next, so what you can do is you can create a directory inside of the pages directory, so pages/api and any file under there will become an API route. - Nice. - Which means that you can create the file and the file is not like a React component
and it's a request handler. - Right. - And it's based on Express this request handler, so generally if you ever used Express before you do like .get() and then like the route that you want to do, so like .get('/', function (req, res) {..}) for example and then you have a handler, a function basically, that gets request and response. - And then like if you, so basically we run into this problem when we were like using Next
and they were like 'Yeah we need this to kind of API but that meant to us like we should start another Node server or you know you try to use the same Node server and you know - Yes. - different setup, different project (Tim laughs) where it's gonna make sense to be in the same place right. - Yeah. So it's actually like that for most people, so what you end up doing is you create, maybe you want to do like microservices or like where every route is a different service or a different file or something like that
however what you end up doing is, if you want to like run your whole projects you have to boot up like maybe ten servers right and you have to run them on different ports and then like try to stitch them together in some way. - And I can only imagine like if you're working in development, in your computer, - Yes so and you're in development you need to run everything - and like you're making changes, and you're refreshing the, you're like running Curl to get, to try out your API and then like you're like, I made this change why doesn't it work,
and then like half an hour later, you figure out that you had to restart the server for example - Right. - Right. - So in Next we solve this by providing, like if you make a change it's immediately updated, so if you do a new request it's immediately going to be the new code that you wrote, and besides that, everything runs in one server in development, however if you deploy it, you deploy as serverless functions.
So this means that every page that you, serverless API route that you make basically it will be serverless and it will be independently ran and independently deployed. - Right and like if I have, like the question that comes to my mind here is that how is that different from the old approach of having a monolith? Because some people will say ok you're writing everything inside the same project
Video Details
- Duration
- 6:53
- Published
- August 13, 2020
- Channel
- Prismic
- Language
- ENGLISH
- Views
- 2,644
- Likes
- 30
Related Videos

How to use Next.js with other frameworks and tools ft Tim Neutkens | Prismic
Prismic
Interviewed: Tim Neutkens

Next js 15 with Jimmy Lai and Tim Neutkens
Software Engineering Daily
Interviewed: Tim Neutkens

16. Pioneers. with Tim Neutkens Next.js lead and co-author
Catch the Tornado
Interviewed: Tim Neutkens

What’s Next for Next.js with Tim Neutkens
Syntax
Interviewed: Tim Neutkens