All Projects → deriegle → express-hotwire

deriegle / express-hotwire

Licence: MIT license
Express tooling for working with Hotwire (HTML over the wire) https://hotwire.dev

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
EJS
674 projects
CSS
56736 projects

Projects that are alternatives of or similar to express-hotwire

Express Openapi Validator
🦋 Auto-validates api requests, responses, and securities using ExpressJS and an OpenAPI 3.x specification
Stars: ✭ 436 (+1017.95%)
Mutual labels:  express-middleware
Express Joi Validation
validate express application inputs and parameters using joi
Stars: ✭ 70 (+79.49%)
Mutual labels:  express-middleware
I18next Express Middleware
[deprecated] can be replaced with i18next-http-middleware
Stars: ✭ 195 (+400%)
Mutual labels:  express-middleware
Connect Gzip Static
connect middleware for statically compressed files
Stars: ✭ 39 (+0%)
Mutual labels:  express-middleware
Jebena
Lightweight JSON validation library
Stars: ✭ 56 (+43.59%)
Mutual labels:  express-middleware
Nodejs Ecommerce Store
An ecommerce store built in NodeJS
Stars: ✭ 114 (+192.31%)
Mutual labels:  express-middleware
Webpack Hot Server Middleware
🔥 Hot reload webpack bundles on the server
Stars: ✭ 319 (+717.95%)
Mutual labels:  express-middleware
Connect Api Mocker
Connect middleware that creates mocks for REST APIs
Stars: ✭ 232 (+494.87%)
Mutual labels:  express-middleware
Webauthn
W3C Web Authentication API Relying Party for Node.js and Express
Stars: ✭ 61 (+56.41%)
Mutual labels:  express-middleware
Host Validation
Express.js middleware for "Host" and "Referer" header validation to protect against DNS rebinding attacks.
Stars: ✭ 183 (+369.23%)
Mutual labels:  express-middleware
Celebrate
A joi validation middleware for Express.
Stars: ✭ 1,041 (+2569.23%)
Mutual labels:  express-middleware
Express Fileupload
Simple express file upload middleware that wraps around busboy
Stars: ✭ 1,069 (+2641.03%)
Mutual labels:  express-middleware
Vue Shoppingcart
ShoppingCart (Ecommerce) 🛒 Application using Vuejs, + Node.js + Express + MongoDB 🚀🤘
Stars: ✭ 141 (+261.54%)
Mutual labels:  express-middleware
Node Iframe Replacement
An alternative to sticking that lovely web app you just built into an <iframe> on a corp website
Stars: ✭ 37 (-5.13%)
Mutual labels:  express-middleware
Limitrr
Light NodeJS rate limiting and response delaying using Redis - including Express middleware.
Stars: ✭ 203 (+420.51%)
Mutual labels:  express-middleware
Mongo Express
Web-based MongoDB admin interface, written with Node.js and express
Stars: ✭ 4,403 (+11189.74%)
Mutual labels:  express-middleware
Join Io
join files on a fly to reduce requests count
Stars: ✭ 80 (+105.13%)
Mutual labels:  express-middleware
Express Basic Auth
Plug & play basic auth middleware for express
Stars: ✭ 241 (+517.95%)
Mutual labels:  express-middleware
Express Gateway
A microservices API Gateway built on top of Express.js
Stars: ✭ 2,583 (+6523.08%)
Mutual labels:  express-middleware
Express Prom Bundle
express middleware with standard prometheus metrics in one bundle
Stars: ✭ 172 (+341.03%)
Mutual labels:  express-middleware

Express library for working with Hotwire (HTML over the wire)

This project was built to provide tooling for working with the new @hotwired/turbo package built by Basecamp for their NEW MAGIC used with hey.com. I wanted to try building similar tooling in express as an example for how this can implemented in other languages, but also to help me learn how it works under the hood.

Using the library

  1. Add the express-hotwire package to your project

Using npm

npm install express-hotwire

Using yarn

yarn add express-hotwire
  1. Use the middleware with your app
const express = require('express');
const expressHotwire = require('express-hotwire');

const app = express();

app.use(expressHotwire());
  1. Render your turbo stream responses using the res.turboStream object.
app.post('/messages', async (req, res) => {
  const { content } = req.fields;

  // create message and save in database/memory/etc
  const message = create(content);

  // Make sure the first argument matches the HTML element id that you want to append a child to
  await res.turboStream.append('messages', {
    partial: 'messages/show', // This should be inside your views directory as views/messages/show.ejs
    locals: {
      // Add any variables needed to render the partial
      message,
    },
  });
});

Turbo Stream Messages and Actions

We provide helpful methods for all the actions listed in the hotwire.dev docs.

// The contents of the partial will be appended to the element with DOM ID "messages".
res.turboStream.append('messages', {
  partial: 'messages/show',
  locals: {
    message: { id: 1, content: 'Hi' },
  },
});

// The contents of the partial will be prepended to the element with the DOM ID "messages".
res.turboStream.prepend('messages', {
  partial: 'messages/show',
  locals: {
    message: { id: 1, content: 'Hi' },
  },
});

// The contents of the partial will replace the existing element with the DOM ID "message_1".
res.turboStream.replace('message_1', {
  partial: 'messages/show',
  locals: {
    message: { id: 1, content: 'Hi' },
  },
});

// The contents of the partial will update the element with DOM ID "unread_count".
res.turboStream.update('unread_count', {
  partial: 'messages/show',
  locals: {
    message: { id: 1, content: 'Hi' },
  },
});

// The element with DOM ID "message_1" will be removed.
res.turboStream.remove('message_1');

Sending Multiple TurboStreams

Sometimes you want to return multiple turbo streams from a request handler. You can use the res.turboStream.multiple function to do that. You have access to all the same turboStream methods in the callback as defined above.

Note: You cannot call turboStream.multiple from within the callback. We don't support nested calls to .multiple

await res.turboStream.multiple((turboStream) => [
  turboStream.append('messages', {
    partial: 'messages/show',
    locals: {
      message: { id: 1, content: 'Hi' },
    },
  }),
  turboStream.prepend('messages', {
    partial: 'messages/show',
    locals: {
      message: { id: 1, content: 'Hi' },
    },
  }),
  turboStream.replace('message_1', {
    partial: 'messages/show',
    locals: {
      message: { id: 1, content: 'Hi' },
    },
  }),
  turboStream.update('unread_count', {
    partial: 'messages/show',
    locals: {
      message: { id: 1, content: 'Hi' },
    },
  }),
  turboStream.remove('message_1'),
]);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].