How To Develop and Build Next.js App with NodeJS Backend
Learn How you develop and build with an example project

There are so many ways we can build Next.js apps and ship them for production. One way is to build the Next.js app with NodeJS or Java and another way is to build the Next.js and serve that static content with NGINX web server. With NodeJS we have to deal with the server code as well, for example, you need to load the index.html page with the node.
In this post, we will see the details and implementation with the NodeJS. We will go through step by step with an example.
- Introduction
- Prerequisites
- Example Project
- Just Enough NodeJS For This Project
- Development Phase
- How To Build Project For Production
- Summary
- Conclusion
Introduction
Next.js is the React framework for static websites, the JAMstack, Production, Desktop, Lightweight Apps, Pre-rendered apps Mobile Web, etc. There are so many advantages using Next.js for your react application such as Pre-Rendering made easier, Exporting a static website with a single command, including CSS in JS code, very less configuration, fully extensible, and optimized for smaller builds, etc. You can get more information on their website here.
React.js is a library, not a framework so that you have to lot of things on your own and there is no specific way to do these as React is not opinionated like Angular. In this post, you can learn how fast we can develop React web application with the Next.js framework.
Next.js is a javascript framework for building web apps and it doesn’t load itself in the browser. We need some kind of mechanism that loads the index.html (single page) of the Next.js application with all the dependencies(CSS and js files) in the browser. In this case, we are using the node as the webserver which loads Next.js assets and accepts any API calls from the Next.js UI app.

If you look at the above diagram all the web requests without the /api will go to Next.js routing and the Next.js Router kicks in and loads components based on the path. All the paths that contain /api will be handled by the Node server itself.
In this post, we are going to develop the Next.js app with NodeJS and see how to build for production.
Prerequisites
There are some prerequisites for this article. You need to have nodejs installed on your laptop and know-how http works. If you want to practice and run this on your laptop you need to have these on your machine.
Example Project
This is a simple project which demonstrates developing and running the Next.js application with NodeJS. We have a simple app in which we can add users, count, and display them at the side, and retrieve them whenever you want.

As you add users we are making an API call to the nodejs server to store them and get the same data from the server when we retrieve them. You can see network calls in the following video.

Here is a Github link to this project. You can clone it and run it on your machine.
// clone the project
git clone https://github.com/bbachi/nextjs-nodejs-example.git// strat the api
cd api
npm install
npm run dev// start the nextjs app
cd my-app
npm install
npm run start:devJust Enough NodeJS For This Project
If you are new to NodeJS don’t worry this short description is enough to get you started with this project. If you are already aware of NodeJS, you can skip this section.
NodeJS is an asynchronous event-driven javascript runtime environment for server-side applications. The current version of the nodejs is 12 and you can install it from this link here. You can click on any LTS link and the NodeJS package is downloaded and you can install it on your laptop.
You can check the version of the Node with this command node -v. You can run javascript on the node REPL by typing the command node on the CLI.

The installation of a node is completed and you know how to run javascript. You can even run javascript files. For example, Let’s put the above commands in the sample.js file and run it with this command node sample.js.











