I have an application that is served from an express server. I am building all of my templates in vanilla JavaScript without any template files or view engine. I want to pass data that will be available in the client's JavaScript context for normal navigation requests. What is the best practice for doing so?
I am not using AJAX or a single page application at all. All of my templates are fully rendered on the server. Here is a basic version of the server:
const express = require('express')
const app = express()
const meta = `<meta name="viewport" content="width=device-width, initial-scale=1">`
const account = `<button id="sign-out" class="js-authorized">Sign Out</button>`
const template = (title, content) => `<html>
<head>
<title>${title}</title>
${meta}
</head>
<body>
<div class='account'>
${account}
</div>
<div class='content'>
<h1>${title}</h1>
${content}
</div>
</body>`
app.get('/', (request, response) => {
const document = template('Index', 'Welcome')
// How can I send this to the client?
const data = {
mainSeed: Math().random(),
secondarySeeds: [Math().random(), Math().random(), Math().random()]
}
// This wouldn't work:
// return response.send(data, document)
return response.send(document)
})
I want to ensure that the data will be accessible to any JavaScript on the page, but I don't want to use any templating logic other than JavaScript template literals on the server. What is the best practice for sending data to the client via basic Express?