Skip to content

Instantly share code, notes, and snippets.

@aral
Forked from developit/htm-ssr-direct.js
Created July 19, 2022 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aral/6fa5e415b11175e92ee2b67af99e79e0 to your computer and use it in GitHub Desktop.
Save aral/6fa5e415b11175e92ee2b67af99e79e0 to your computer and use it in GitHub Desktop.
const express = require('express');
const htm = require('htm');
const vhtml = require('vhtml');
// create an html`` tag function for vhtml:
const html = htm.bind(vhtml);
const App = (props) => html`
<div class="app">
<h1>This is an app</h1>
<p>Current server time: ${new Date + ''}</p>
</div>
`;
const app = express();
app.get('/', (request, response) => {
// html already returns a string!
const body = html`<${App} url=${request.url} />`;
// wrap it in an HTML document and send it back
response.send(`<!DOCTYPE html><html><body>${body}</body></html>`);
});
{
"dependencies": {
"htm": "^2.2.1",
"vhtml": "^2.1.0",
"express": "^4.17.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment