• Mail us
  • Book a Meeting
  • Call us
  • Chat with us

ReactJS

Building APIs in React.js with Express and Server Side Rendering


Introduction

React.js itself does not support server-side rendering (SSR) APIs such as Next.js. Nevertheless, you can implement similar functionality by combining Express.js with React to build API routes and manage SSR.

Here's how you can implement APIs in React.js with SSR using Express.js:

Step 1: Set Up React with Express

1. Create your project:

npx create-react-app react-ssr-appcd react-ssr-appnpm install express react-dom/server

 

2. Project Structure:

react-ssr-app/ ├── public/ ├── src/ │ └── App.js ├── server/ │ └── server.js // Express server for SSR & API routes ├── package.json ├── ...

 

Step 2: Create Express Server for SSR & API

server/server.js

import express from 'express';import path from 'path';import { fileURLToPath } from 'url';import { renderToString } from 'react-dom/server';import React from 'react';import App from '../src/App.js';const __filename = fileURLToPath(import.meta.url);const __dirname = path.dirname(__filename);const app = express();const PORT = process.env.PORT || 3000;// Serve static filesapp.use(express.static(path.resolve(__dirname, '../build')));// Example API Routeapp.get('/api/data', (req, res) => { res.json({ message: 'Hello from SSR API!' });});// SSR Routeapp.get('*', (req, res) => { const appString = renderToString(<App />); const html = ` <!DOCTYPE html> <html> <head> <title>React SSR</title> <link rel="stylesheet" href="/static/css/main.css" /> </head> <body> <div id="root">${appString}</div> <script src="/static/js/bundle.js"></script> </body> </html> `; res.send(html);});// Start Serverapp.listen(PORT, () => { console.log(`SSR Server is running on http://localhost:${PORT}`);});

 

Step 3: Build and Serve the App

1. Build React App:

npm run build

 

2. Run the Express Server:

node server/server.js

 

3. Test the API Route:

Go to http://localhost:3000/api/data, You should see:

{ "message": "Hello from SSR API!" }

 

4. Test the SSR Route:

Go to http://localhost:3000, Your React app should be server-rendered.

 

Using TypeScript + React + Express (SSR)

You can improve this configuration by utilizing TypeScript and Babel for improved type safety, or even by incorporating tools like Vite for improved build times.

 

Step 5: Initialize next-i18next

To achieve Next.js-type APIs with SSR in React.js, you can integrate React with an Express.js server. Express serves both the API routes and server-side rendering (SSR) of your React components.

  • API Routes: Managed through Express (e.g., /api/data).
  • SSR: Utilizes react-dom/server's renderToString to render pre-rendered HTML.
  • Static Assets: Served from the React build directory.

This solution enables you to build a custom SSR configuration like Next.js but with full server logic control.

Ready to transform your business with our technology solutions? Contact Us today to Leverage Our ReactJS Expertise.

0

Share

facebook
LinkedIn
Twitter
Mail
React

Related Center Of Excellence