1

I have recently begun migrating a project made with create-react-app to next.js, and I am not sure how I should migrate the backend of it.

I currently have an express server as my backend, but I see in next.js there are performance advantages by using serverless functions in the api folder. My question is: am I better off migrating the express server to serverless functions or keeping it and using it with next.js? Also, is it a bad idea to use express in a serverless function?

I have looked at quite a few tutorials on next.js and many use express although the next.js docs recommend against this, so I'm not sure if I'm missing some advantage here.

[EDIT]: my backend basically consists of routes for getting/updating database content and uploading files, with JWT authentication middleware.

isaacholt100
  • 388
  • 3
  • 11
  • How complex is your backend and are you using any middleware? In general, this is going to be your personal preference. I personally prefer using next's api folder, I feel like it makes the development experience and deployment better. I also really like using vercel for hosting my personal stuff, because it has an amazing free tier that makes continuous integration easy. – kyle Sep 18 '20 at 23:04

1 Answers1

1

I try to avoid using express.js. Not only for the performance benefits, but also because it's one more layer of debugging and maintenance to have to worry about.

However, there are some circumstances which will still force you to depend on express:

  1. Some (especially older) plugins will rely on it. For example, until very recently, the next-18next plugin for multi-language support needed express.js to work. This might be why some tutorials and examples will be using express too, if they are using such a plugin. Fortunately, in a recent update, the next-i18next team managed to remove this requirement and you can use the plugin without depending on express. Hopefully over time, more plugins will follow suit, though will be time before the tutorials, docs and examples catch up.
  2. Depending on your hosting set-up, you may need express.js to manage caching of your assets. If you're hosting on Vercel, which is built specifically for hosting next.js apps, then this won't be an issue, as you can manage the hosting in that console, which in my opinion is also preferable to the idea of having to maintain an express server.

In summary, if you're hosting on Vercel or don't need to worry about asset caching, and aren't using any plugins which rely on express.js, then setting up express is a waste of energy.