Server-Side Rendering (SSR) is a technique that generates HTML on the server rather than on the client. With SSR, Express.js can serve dynamic content, enhancing both SEO and load performance by delivering content that search engines and users can immediately view.
In traditional client-side rendering (CSR), JavaScript code runs in the browser to render pages. With SSR, however, HTML is generated on the server and sent to the client as a fully-rendered page. This approach has advantages in terms of SEO and initial load speed, particularly for content-heavy websites.
Example:
The main benefits of SSR are:
To start implementing SSR, you need a basic Express.js server setup that will handle rendering HTML.
mkdir express-ssr
cd express-ssr
npm init -y
npm install express
server.js
)
const express = require('express');
const app = express();
app.use(express.static('public')); // Serves static files like CSS and JavaScript
app.get('/', (req, res) => {
res.send('Hello from Server-Side Rendering with Express!
');
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));
To render dynamic HTML, we’ll use templating engines such as EJS, Pug, or Handlebars. Express provides built-in support for these engines, making SSR straightforward.
npm install ejs
server.js
)
const express = require('express');
const app = express();
app.set('view engine', 'ejs');
app.set('views', './views'); // Folder where EJS templates are stored
app.use(express.static('public'));
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));
With EJS, you can dynamically inject data into HTML templates. Let’s create a simple web page that renders a dynamic message.
In the views
folder, create a file called home.ejs
:
SSR with EJS
Welcome, <%= name %>!
This page is rendered on the server with EJS.
Update the server.js
file:
const express = require('express');
const app = express();
app.set('view engine', 'ejs');
app.set('views', './views');
app.get('/', (req, res) => {
res.render('home', { name: 'User' });
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));
When you navigate to http://localhost:3000
, the server sends back the HTML
SSR with EJS
Welcome, User!
This page is rendered on the server with EJS.
This simple example shows how you can dynamically inject data into HTML templates with EJS and serve it directly from Express.
In addition to EJS, you can also use modern frontend frameworks like React to implement SSR. For this setup, we’ll need additional packages.
Create a basic React component in components/Home.js:
const React = require('react');
function Home({ name }) {
return (
Welcome, {name}!
This page is server-side rendered with React and Express.
);
}
module.exports = Home;
npm install react react-dom server
In server.js
, add code to render this component server-side:
const express = require('express');
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const Home = require('./components/Home');
const app = express();
app.get('/', (req, res) => {
const component = ReactDOMServer.renderToString(React.createElement(Home, { name: 'User' }));
const html = `
SSR with React
${component}
`;
res.send(html);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));
Output: When you load the page, the server sends back fully-rendered HTML for React components.
With Server-Side Rendering (SSR) in Express.js, you can deliver fast, SEO-friendly content directly to the client. Using EJS for templating is a straightforward approach to SSR, while frameworks like React enhance interactivity and scalability. Combining SSR with data fetching, hydration, and caching provides a powerful setup for responsive, user-friendly applications. Happy Coding!❤️