Should I develop a separate express server, or handle all API calls in my next.js app?

Viewed 4143

My website will perform CRUD operations and will work with MongoDB and Firebase storage+auth.

What are the reasons / advantages to developing a separate Express server instead of integrating everything in my next.js app?

As far as I have seen, it can all be done in my next.js app, but I still see many projects working with a separate server.

5 Answers

Depends on what your app does and how you are hosting it.

Running Next.Js on a standard server will be of little difference whether you are using nextjs's /api or expressjs.

However if you are hosting on serverless (e.g. Vercel), I would recommend using a separate express server if you have alot of CRUD operations because the warming up of serverless is really bad user experience.

Build and Deployment

Next/JS - If you want to edit something on the backend, and push the changes, it will require you to build the entire JS app, and depending on how big is your app, it can take alot of time (especially if alot of static generated pages).

Express - If you running express separately, you can build and deploy front end and backend separately. It's time saving, and you can also better organise your codes frontend/backend.

Choice of deployment

I have a choice to take advantage of Vercel to host my frontend, with static generated pages and some server side generated pages (automatic scaling, caching, CDN etc), and host my backend with a separate cluster of servers.

PS: I moved from single Next.JS app to NextJs+Express

I can think of a few things why they would have a different server from the one NextJS provides:

  1. Familiarity with Express, Koa, etc. All next-connect helps with this
  2. There is an already existing API in PHP, Express, Flask, etc.

It is literally based on what you would want to do, the extra interactions with MongoDB & Firebase would be same on both the technologies, unless you want to isolate respective things separately, I don't see any harm in doing everything together on next.

Given that the idea of using next.js, as per my understanding would be to utilise server side rendering.

I've been using Next.js with Typescript for quite a while now and I, as of now, have found one reason not to include express.js in my project. And the reason is Vercel.

Since I use Vercel for continuous deployment of my projects, and Vercel Not supporting any custom server as of there Docs here, I refrain from using Express or any other custom servers.

I didn't face any problem performing CRUD operations with MongoDB, can't say about firebase.

On Next.js Docs, I found these points to be considered:

  1. A custom server can not be deployed on Vercel, the platform Next.js was made for.
  2. A custom server will remove important performance optimizations, like serverless functions and Automatic Static Optimization.

But at the end of the day it very personal opinion weather to use a custom server or not. It might depend on a very specific use case you might be looking for.

Personally, I try to keep it to just NextJS, but if I have to manage real-time data with Socket.io, I get a separate server because other than WebSockets, serverless functions can do everything else.

Related