Ep.10: Next.js (Basic app for Node Backend Frameworks Comparison)
Teolin Codreanu
Software Developer | Microservices / APIs in Node.js, JavaScript, TypeScript, Nest.js / Koa / Express etc
This article is part of an in-depth comparison series of the top Node frameworks. We'll cover all the important aspects: market share, learning curve, ecosystem, security and more. To compare them properly, we will build the exact same app in all these frameworks (plus Vanilla Node), observe all the steps along the way and then benchmark them as we progressively add more functionalities.
Table of contents
What's next? Haha, lame joke, I know..., but yeah, Next.js is the last framework from the Whales pod. Not to be confused with Nest.js. Or Nuxt.js. I do believe the whole naming shenanigans is a conspiracy to get us very confused, more so as the three back-end frameworks, Nest, Next and Nuxt, are usually paired with the 3 titans of the frontend: Angular, React, Vue.
The thing is, Next is not actually a back-end framework. It calls itself a full-stack framework, but it's mostly focused on Server Side Rendering, either static, dynamic or incremental. It has a router and can do light backend logic, but that's not its main focus. It might be difficult to test it along with the others. but we'll see.
Anywho, what does Next bring to the table? According to its own website, quite a few goodies:
That's quite neat, and together with the massive popularity of React, it explains why Next is second only to Express.
Analysis (Next.js)
Next.js is a versatile framework primarily used for building web applications, providing an intuitive and efficient environment for developers.
Show me the code
First, we need to initiate a new Next project, and it comes with its own CLI. Or you can simply do this:
npx create-next-app@latest todo-next
I left everything on default, including the TypeScript preference, except for src directory, which I do want to use.
It seems we'll need to follow the Next.js way of structuring APIs and pages. In Next.js, you create API routes under the pages/api directory, and inside that folder, there should be a folder structure that mimics the route structure:
领英推荐
my-next-app/
├── pages/
│ ├── api/
│ │ ├── users/
│ │ │ ├── register.ts
│ │ │ ├── user/[id].ts
│ │ ├── tasks/
│ │ │ ├── create.ts
│ │ │ ├── task/[id].ts
│ │ │ ├── task/[id]/complete.ts
│ │ ├── lists/
│ │ │ ├── create.ts
│ │ │ ├── list/[id].ts
└── ...
Now, that's a lot of clicking to create all those folders and files, so a faster way is with some shell commands:
#!/bin/bash
mkdir -p src/pages/api/users/user
mkdir -p src/pages/api/tasks/task/[id]
mkdir -p src/pages/api/lists/list
touch src/pages/api/users/register.ts
touch src/pages/api/users/user/[id].ts
touch src/pages/api/tasks/create.ts
touch src/pages/api/tasks/task/[id].ts
touch src/pages/api/tasks/task/[id]/complete.ts
touch src/pages/api/lists/create.ts
touch src/pages/api/lists/list/[id].ts
Routing and Controllers
There's no router per se and no controllers like we had in the other frameworks, only a handler function for each route. The routing is done automatically, based on the folder structure. For example, this file:
├── pages/
│ ├── api/
│ │ ├── users/
│ │ │ ├── register.ts
... would be routed to /api/users/register.ts. Catch all parameters, like :id, would be represented in square brackets, like so:
├── pages/
│ ├── api/
│ │ ├── users/
│ │ │ ├── user/[id].ts
The handler functions look very similar to our usual controllers. For example, the register route would look like this:
This is the typical handling in Next. It has some extra error handling that we did not implement yet in the other frameworks, for "Method not allowed". The extra conditional might have a minimal performance impact compared, so I might remove that, to make sure we're comparing the exact same basic app.
The other routes are pretty much identical, so no point going through all of them here, just have a look at the repo.
Services
No changes are necessary to the services compared to our baseline, Express, except for the fact that Next is TypeScript native. I will address that later, as I want to also create a TS version for Express as well.
Front-end
We don't need it, but Next is a full-stack framework, so it comes with front-end included. Actually, it can do way more on the front-end than it can do in the back-end, but that's another story.
Well, that's all folks, our app is up and running, and we're ready to move on.