Answer:
- You do not need to use
express
to use nextJS out of the box; the vast majority of people don't.
Long Answer:
If you are not hosting on Vercel's platform, for example self-hosting (AWS,Digital Ocean, Azure etc...) you optionally have the ability to define a custom nextjs server and swap the underlying server implentation/framework (to use express, fastify, vanilla node etc..).
When might a custom server be a good idea?
- when you're not hosting on vercel
- when you have custom requirements & custom existing infrastructure
Scenario:
Imagine a large company that has lots of custom built infrastructure (logging, AB testing, custom linters etc) they've built over the years. They now want to take advantage of some of NextJS abstractions like:
- different SSR rendering strategies
- modern/flexible build system
- all the other stuff you would need to implement from scratch, maintain, test...
Let's call this company XCompany. XCompany uses ExpressJS (doesn't matter which node server/framework) for their web server. They want to continue using ExpressJS since they've invested lots of time, resources building custom infrastructure and tools to work with it like AB testing integration, logging middlewares etc.
A custom server would allow XCompany to continue using Express without a complete re-write change and still benefit from the stuff NextJS that next offers SSR, the build system etc, nice conventions & guardrails etc.
At end of this response I linked to Lyft's engineering blogpost of their migration to using NextJS with their own infra
When you self-host your server and deploy it to your on infrastructure you get a long running node server, which is different from hosting on vercel, where the server is a serverless function.
Context/History
NextJS under the hood uses vanilla HTTP server. If you would like to get similar express
functionality like the way routes are setup/organized you can use a package like next-connect
.
Express use to rely on the connect
package directly. Most nodejs servers follow connect
like API for routing & setting up handlers.
For folks coming outside of the NodeJS world (python/flask), the connect style of organizing server routes is sort of like WASGI - Web Server Gateway Interface in philosophy.
Cons/Challenges of using Custom NextJS Server?
- initial time investment learning custom nextJS server patterns.
- The vast majority of people don't go down this route so documentation is more scarce
- if you're not hosting on vercel you don't get serverless functions. This may not be con or downside, if you don't have a serverless use case.
- For example, if you're web server will be using websockets/WebtRTC which require persistent long running connections
between client and server than serverless function's arent the best
choice here; there are work arounds, but i still think its not the
best use case. Note, you can still use add on serverless features to your custom server through another provider (AWS, Azure, etc...)
Moving Forward
Most js runtimes have been converging on exposing & using the Request
& Response
object APIs and exposing them to consumers.
Note, there's a ton of stuff that could be talked about here. Please leave follow up questions & I will try to update this answer accordingly
Resource(s)