Search code examples
reactjsmongodbfirebaseexpressnext.js

Should I develop a separate Express server or handle all API calls in my Next.js app?


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.


Solution

  • 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 Next.js's API or Express.js.

    However if you are hosting on serverless (e.g. Vercel), I would recommend using a separate Express server if you have a lot 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 a lot of time (especially if alot of static generated pages).

    Express.js - If you running Express separately, you can build and deploy front end and backend separately. It's time saving, and you can also better organize 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 Next.js + Express.