{"id":17055109,"url":"https://github.com/deriegle/new-magic-expressjs","last_synced_at":"2025-06-12T13:06:24.387Z","repository":{"id":84059694,"uuid":"324102768","full_name":"deriegle/new-magic-expressjs","owner":"deriegle","description":"Trying out the NEW MAGIC (Hotwire) using Node.js and Express","archived":false,"fork":false,"pushed_at":"2020-12-24T20:46:47.000Z","size":47,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-10T07:56:06.605Z","etag":null,"topics":["express","expressjs","hotwire","javascript","nodejs","rails","turbo","turbolinks"],"latest_commit_sha":null,"homepage":"https://hotwire.dev","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/deriegle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2020-12-24T08:10:28.000Z","updated_at":"2024-07-03T08:07:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"1add1903-3f00-4a69-958a-29ca146d1b6c","html_url":"https://github.com/deriegle/new-magic-expressjs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/deriegle/new-magic-expressjs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deriegle%2Fnew-magic-expressjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deriegle%2Fnew-magic-expressjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deriegle%2Fnew-magic-expressjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deriegle%2Fnew-magic-expressjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deriegle","download_url":"https://codeload.github.com/deriegle/new-magic-expressjs/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deriegle%2Fnew-magic-expressjs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259470938,"owners_count":22862995,"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":["express","expressjs","hotwire","javascript","nodejs","rails","turbo","turbolinks"],"created_at":"2024-10-14T10:16:46.339Z","updated_at":"2025-06-12T13:06:24.356Z","avatar_url":"https://github.com/deriegle.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Trying out NEW MAGIC in Node.js\n\nThis project is using the new `@hotwired/turbo` package built by Basecamp for their **NEW MAGIC** used with hey.com. I wanted to try to figure out how it works behind the scenes after trying it out in Rails. There is a lot of magic going on behind the scenes like most Rails features. I figured building it in Node.js/Express would help me understand it more.\n\nI've built this application to be similar to the sample application that is built in the video on [hotwire.dev](https://hotwire.dev). Make sure to check out that video if you haven't.\n\nThe primary goal was to learn and to provide a clear example to others on how the magic works and how it can be used in different programming languages.\n\n## Facts about this project:\n\n - We are using an in-memory model of a Message that we interact with on the page.\n - Custom middleware built for Express that makes sending turbo-stream responses super easy.\n\n ## Using the custom middleware in `./turboStream.js`\n\n 1. Import and use the middleware\n\n ```js\n const express = require('express');\n const turboStream = require('./turboStream');\n\n const app = express();\n\n app.use(turboStream())\n ```\n\n 2. Render your turbo stream response\n\n **Append**\n\n```js\napp.post('/messages', (req, res) =\u003e {\n    const { content } = req.fields;\n\n    // create message and save in database/memory/etc\n    const message = create(content)\n\n    // Make sure the first argument matches the HTML element id that you want to append a child to\n    res.turboStream.append('messages', {\n        partial: 'messages/show', // This should be inside your views directory as views/messages/show.ejs\n        locals: { // Add any variables needed to render the partial\n            message,\n        }\n    });\n})\n```\n\n**Prepend**\n\n```js\napp.post('/messages', (req, res) =\u003e {\n    const { content } = req.fields;\n\n    // create message and save in database/memory/etc\n    const message = create(content)\n\n    // Make sure the first argument matches the HTML element id that you want to prepend a child to\n    res.turboStream.prepend('messages', {\n        partial: 'messages/show', // This should be inside your views directory as views/messages/show.ejs\n        locals: { // Add any variables needed to render the partial\n            message,\n        }\n    });\n})\n```\n\n**Replace**\n\n```js\napp.post('/messages/:messageId', (req, res) =\u003e {\n    const { messageId } = req.params;\n    const { content } = req.fields;\n\n    // update message in database/memory/etc\n    const message = updateById(messageId, content)\n\n    // Make sure the first argument matches the HTML element id that you want to replace\n    res.turboStream.replace(`message_${messageId}`, {\n        partial: 'messages/show', // This should be inside your views directory as views/messages/show.ejs\n        locals: { // Add any variables needed to render the partial\n            message,\n        }\n    });\n})\n```\n\n**Update**\n\n```js\napp.get('/messages/refresh', (req, res) =\u003e {\n    // Make sure the first argument matches the HTML element id that you want to update\n    res.turboStream.update('messages', {\n        partial: 'messages/index', // This should be inside your views directory as views/messages/index.ejs\n        locals: { // Add any variables needed to render the partial\n            messages: getMessagesFromDatabase(),\n        }\n    });\n})\n```\n\n **Remove**\n\n ```js\napp.post('/messages/:messageId/delete', (req, res) =\u003e {\n    const { messageId } = req.params;\n\n    // delete message from database/memory/etc\n\n    // Make sure this matches the HTML element id that you want to remove from the DOM\n    res.turboStream.remove(`message_${messageId}`);\n})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderiegle%2Fnew-magic-expressjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fderiegle%2Fnew-magic-expressjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderiegle%2Fnew-magic-expressjs/lists"}