Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 558
Oz Heymann Avatar asked May 02 '21 16:05

Oz Heymann


People also ask

How to connect to an API with JavaScript?

How To Connect to an API (with JavaScript) 1 1. Get an API key. An API key is usually a unique string of letters and numbers. In order to start working with most APIs, you must identify yourself ... 2 2. Test API Endpoints with JavaScript. 3 3. Make your first JavaScript app with API.

Which init system should I use for my Express app?

The two main init systems in use today are systemd and Upstart. There are two ways to use init systems with your Express app: Run your app in a process manager, and install the process manager as a service with the init system.

How to use JAAS API in RapidAPI?

This API is used to generate and modify JSON objects, that is useful in coding practice. In order to find JAAS API section, enter its name in the search box in the RapidAPI service or go to the “Data” category from “All Categories” list and select this API from the list.

How to improve the performance of Your Express app?

Accordingly, the information is divided into two parts: Here are some things you can do in your code to improve your application’s performance: Gzip compressing can greatly decrease the size of the response body and hence increase the speed of a web app. Use the compression middleware for gzip compression in your Express app.


Video Answer


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

like image 178
Someone Special Avatar answered Oct 25 '22 01:10

Someone Special


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.
like image 28
Teej Avatar answered Oct 25 '22 00:10

Teej


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.

like image 37
Gandalf the White Avatar answered Oct 24 '22 23:10

Gandalf the White


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.

like image 28
Ashutosh kumar Avatar answered Oct 25 '22 01:10

Ashutosh kumar


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.

like image 20
Krish Garg Avatar answered Oct 25 '22 01:10

Krish Garg