To demonstrate server-side rendering for showing flight information, I will use Node.js with Express as the server framework, Socket.IO for real-time data transfer, and Handlebars as the template engine. We'll store flight information in a simple in-memory data store, but you can easily adapt it to use a real database.
- Install the required packages:
npm install express socket.io express-handlebars
- Now let's create
app.js:
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const exphbs = require('express-handlebars');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
// Set up Handlebars as the template engine
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
// In-memory data store for flight information
const flightData = {};
// Function to update flight data
function updateFlightData(flight, data) {
flightData[flight] = data;
// Emit an event to all connected clients with the flight data
io.emit('flightData', { flight, data });
}
// Serve the flight data entry page
app.get('/', (req, res) => {
res.render('dataEntry', { flightData });
});
// Serve the flight data display page
app.get('/display', (req, res) => {
res.render('flightDisplay', { flightData });
});
// Set up Socket.IO connection
io.on('connection', (socket) => {
console.log('A user connected');
// Listen for flight data updates from clients
socket.on('updateFlightData', (data) => {
updateFlightData(data.flight, data.data);
});
socket.on('disconnect', () => {
console.log('A user disconnected');
});
});
// Start the server
const port = 3000;
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
In this file, we have set up a basic Express application with Socket.IO for real-time updates. We have two routes, / and /display, which render different views for entering and displaying flight data. When a connected client sends a updateFlightData event, the server updates the data in memory and broadcasts the updates to all other connected clients.
- Create a Handlebars template for the data entry page. Create a file called
views/dataEntry.handlebars:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flight Data Entry</title>
</head>
<body>
<form id="data-entry-form">
Flight number: <input type="text" id="flight-input" />
Flight data: <input type="text" id="data-input" />
<button type="submit">Submit</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
document.getElementById('data-entry-form').addEventListener('submit', (e) => {
e.preventDefault();
const flight = document.getElementById('flight-input').value;
const data = document.getElementById('data-input').value;
socket.emit('updateFlightData', { flight, data });
});
</script>
</body>
</html>
This template renders a simple form where users can enter flight data. When the form is submitted, we emit a updateFlightData event to the server with the flight data.
- Create a Handlebars template for the flight data display page, called
views/flightDisplay.handlebars:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flight Data Display</title>
</head>
<body>
<ul id="flight-display">
{{#each flightData}}
<li id="flight-{{@key}}">{{@key}}: {{this}}</li>
{{/each}}
</ul>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
socket.on('flightData', (data) => {
const flightDisplay = document.getElementById('flight-display');
const flightItem = document.getElementById(`flight-${data.flight}`);
if (flightItem) {
flightItem.innerText = `${data.flight}: ${data.data}`;
} else {
const newFlightItem = document.createElement('li');
newFlightItem.id = `flight-${data.flight}`;
newFlightItem.innerText = `${data.flight}: ${data.data}`;
flightDisplay.appendChild(newFlightItem);
}
});
</script>
</body>
</html>
This template renders a list of flight information. When a flightData event is received from the server, we update the corresponding list item or create a new one if necessary.
- Now start the server:
node app.js
With this code, open two browser windows: one at http://localhost:3000/ and another at http://localhost:3000/display. If you enter flight data in the first window, you should see the flight data get updated in real-time in the second window.