{"id":14970040,"url":"https://github.com/markcellus/html-express-js","last_synced_at":"2025-10-26T11:31:22.290Z","repository":{"id":37212706,"uuid":"503427098","full_name":"markcellus/html-express-js","owner":"markcellus","description":"Render native HTML in Express middleware using JavaScript","archived":false,"fork":false,"pushed_at":"2025-01-01T10:25:40.000Z","size":1445,"stargazers_count":18,"open_issues_count":4,"forks_count":4,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-01-31T18:56:45.799Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/markcellus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-14T15:56:54.000Z","updated_at":"2025-01-01T10:25:44.000Z","dependencies_parsed_at":"2023-01-31T21:45:24.498Z","dependency_job_id":"f3df3671-249f-45ec-8dc8-ffc69571b6e1","html_url":"https://github.com/markcellus/html-express-js","commit_stats":{"total_commits":139,"total_committers":3,"mean_commits":"46.333333333333336","dds":"0.49640287769784175","last_synced_commit":"39b69774eccceb7ca15813a6aa89084916d99e5e"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markcellus%2Fhtml-express-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markcellus%2Fhtml-express-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markcellus%2Fhtml-express-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markcellus%2Fhtml-express-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/markcellus","download_url":"https://codeload.github.com/markcellus/html-express-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238319439,"owners_count":19452337,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T13:42:56.253Z","updated_at":"2025-10-26T11:31:16.974Z","avatar_url":"https://github.com/markcellus.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![build](https://img.shields.io/travis/markcellus/html-express-js)\n![npm](https://img.shields.io/npm/v/html-express-js)\n![node](https://img.shields.io/node/v/html-express-js)\n\n# html-express-js\n\n## Features\n\n- Serves HTML documents using template literals\n- Supports includes in HTML documents\n- Allows shared global state throughout templates\n\n## Installation\n\n```\nnpm install html-express-js\n```\n\n## Basic Usage\n\nThe following is a high level example of how the package can be used as an Express template engine. See [example](/example) directory for all details of a working implementation.\n\nSet up your Express app to use this engine:\n\n```js\nimport htmlExpress, { renderView } from 'html-express-js';\n\nconst app = express();\nconst __dirname = resolve();\n\nconst viewsDir = `${__dirname}/public`;\n\nconst { engine, staticIndexHandler } = htmlExpress({\n  viewsDir, // root views directory to serve all index.js files\n  includesDir: `${viewsDir}/includes`, // OPTIONAL: where all includes reside\n  notFoundView: '404/index', // OPTIONAL: relative to viewsDir above\n});\n\n// set up engine\napp.engine('js', engine);\n\n// use engine\napp.set('view engine', 'js');\n\n// set directory where all index.js pages are served\napp.set('views', viewsDir);\n\n// render HTML in public/homepage.js with data\napp.get('/', function (req, res, next) {\n  renderView('homepage', req, res, {\n    title: 'Awesome Homepage',\n    name: 'Bob',\n  });\n});\n\n// OPTIONALLY: route all GET requests to directories\n// to their associated static index.js views in the public directory\n// and, if not found, route to the 404/index.js view\napp.use(staticIndexHandler());\n```\n\nThen you can create the associated files:\n\n```js\n// public/includes/head.js\nimport { html } from 'html-express-js';\n\nexport const view = () =\u003e html`\n  \u003cmeta charset=\"utf-8\" /\u003e\n  \u003cmeta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" /\u003e\n  \u003cmeta\n    name=\"viewport\"\n    content=\"width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0\"\n  /\u003e\n`;\n```\n\n```js\n// public/homepage.js\nimport { html } from 'html-express-js';\n\nexport const view = (data, state) =\u003e html`\n  \u003c!doctype html\u003e\n  \u003chtml lang=\"en\"\u003e\n    \u003chead\u003e\n      ${state.includes.head}\n      \u003ctitle\u003e${data.title}\u003c/title\u003e\n    \u003c/head\u003e\n\n    \u003cbody\u003e\n      \u003ch1\u003eThis is the homepage for ${data.name}\u003c/h1\u003e\n    \u003c/body\u003e\n  \u003c/html\u003e\n`;\n```\n\n## Advanced usage\n\n### Injecting and using state based on a request\n\nThe following shows an example of showing a logged out state based on the cookie on a request.\n\n```js\nimport htmlExpress, { renderView } from 'html-express-js';\n\nconst app = express();\nconst __dirname = resolve();\n\nconst viewsDir = `${__dirname}/public`;\n\nconst { engine, staticIndexHandler } = htmlExpress({\n  viewsDir,\n  /**\n   * Inject global state into all views based on cookie\n   */\n  buildRequestState: (req) =\u003e {\n    if (req.cookies['authed']) {\n      return {\n        loggedIn: true,\n      };\n    }\n  },\n});\n\napp.engine('js', engine);\napp.set('view engine', 'js');\napp.set('views', viewsDir);\n\napp.get('/', function (req, res, next) {\n  renderView('homepage', req, res);\n});\n```\n\n```js\n// public/homepage.js\nimport { html } from 'html-express-js';\n\nexport const view = (data, state) =\u003e {\n  const { loggedIn } = state;\n\n  return html`\n    \u003c!doctype html\u003e\n    \u003chtml lang=\"en\"\u003e\n      \u003chead\u003e\n        \u003ctitle\u003e${data.title}\u003c/title\u003e\n      \u003c/head\u003e\n\n      \u003cbody\u003e\n        ${loggedIn ? `\u003ca href=\"/logout\"\u003eLogout\u003c/a\u003e` : 'Not logged in'}\n      \u003c/body\u003e\n    \u003c/html\u003e\n  `;\n};\n```\n\n## Development\n\nRun site in examples directory\n\n```bash\nnpm start\n```\n\nRun tests\n\n```bash\nnpm test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkcellus%2Fhtml-express-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarkcellus%2Fhtml-express-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkcellus%2Fhtml-express-js/lists"}