Integrating Express.js Server with Next.js-13: Unleash the Power of Combined Frameworks
Tags: nextjs, express, react, beginners
Published: 2023-06-20 09:25:40


## Introduction:
Next.js-13 and Express.js are two powerful frameworks in the JavaScript ecosystem, each offering distinct advantages. Next.js-13 provides features like server-side rendering (SSR) and static site generation (SSG), along with an excellent developer experience. On the other hand, Express.js is known for its flexibility and robustness in building server-side applications. By combining these frameworks, developers can harness the strengths of both to create versatile web applications. In this blog post, we will explore the steps to connect an Express.js server with Next.js-13 seamlessly.

## Benefits of Next.js-13:
Next.js-13 offers a host of advantages that contribute to its popularity among developers:

Server-side rendering (SSR): Next.js-13 enables rendering React components on the server, leading to faster page loading and improved search engine optimization (SEO).
Static site generation (SSG): It generates static HTML pages during the build process, resulting in optimal performance and reduced server load.
Excellent developer experience: Next.js-13 provides a delightful development experience with features like automatic code splitting, hot module reloading, and built-in TypeScript support.

## Benefits of Express.js:
Express.js is a widely adopted framework for building server-side applications with Node.js. Some key advantages of using Express.js include:

Flexibility: Express.js takes a minimalistic, unopinionated approach, allowing developers to structure applications as per their specific needs.
Middleware ecosystem: Express.js boasts a vast collection of middleware modules, making it easy to add additional functionality to the server.
Robustness: With its mature and stable ecosystem, Express.js has proven to be reliable in handling various server-side tasks and managing routes efficiently.

## Combining Next.js-13 and Express.js:
By integrating an Express.js server with Next.js-13, developers can leverage the best of both worlds. Express.js can handle backend logic, routing, and API endpoints, while Next.js-13 takes care of rendering React components, server-side rendering, and static site generation. This powerful combination empowers developers to build dynamic web applications that benefit from the unique features offered by each framework.

## Steps to Create Both Projects:
First, let's create a basic express server.
1.`mkdir exp-next && cd exp-next`
2.`npm init -y`
3.`npm install express cors`
4.`mkdir src && cd src && touch index.js`

In the package.json file add the line "type":"module", to enable the use of "import" statement and "start": "node src/index.js" script

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwdr8d7pw7b53l8j2bxv.png)

Next, in your index.js file add the following lines to create a basic express server which returns an array of users.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3b78sqbjhrlrtg4t4ku0.png)

Finally `npm start` to run your server. If everything is ok, you will see "App listening on port 5000!" on your console.

Next, we will create out Next.js-13 app. Run the following commands on your command prompt.
1.`mkdir next-exp && cd next-exp`
2.`npx create-next-app@latest`
3.What is your project named? . using . will install nextjs in the current folder with the folder name next-exp
4.Accept all default settings.
5.`npm install --save express`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0nmge5692lbyx0n7ju8.png)

##Next.js Settings

Open the next.config.js file and make the following changes.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32tf90y4zb9690095yva.png)


##Client component

In the app folder, open page.tsx and add the following code.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hkit5k5qqcfw8ljv75kq.png)

Run `npm start` in your express project to start the server and `npm run dev` in your next.js project.

Notice we did not pass any other option to the fetch function, just the url. Nextjs by default uses fetch(url, { cache: 'force-cache' }) which automatically caches our users.

_Note: Next.js recommends the use of SWR or React-Query for data fetching on the client side as against useEffect hook._

##Server component
In the app folder, following open page.tsx and replace the previous code with the following lines.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jaiu63uvdxsnvpn67x21.png)

Here we are passing { cache: 'no-cache' } option to the fetch function to disable Next.js default behaviour and always refetch users when we make a request.

That's it! Now you can enjoy all the goodies Next.js-13 has to offer while keeping your existing express.js backend.
[You can find the code here. ](https://github.com/Wilmela/nextjs-express)

Thanks for reading.