express-handlebars-hotreload
TypeScript icon, indicating that this package has built-in type declarations

0.0.6 • Public • Published

Express Handlebars Hot Reload

🔥 Automatically reload handlebars templates when they change.

Installation

npm install express-handlebars-hotreload

Usage

A simple example:

const exphbs = require('express-handlebars-hotreload');
const express = require('express');
exphbs.hotreload()

const app = express();
const hbs = exphbs.create({
  hotreload: true,
})
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('views', process.cwd() + '/example/views');

app.get('/', (req, res) => {
    res.render('home');
});

app.listen(4241);

Options

If you want to specifiy the websocket port, you can pass an object to the hotreload function and the engine function or the create method.

hotreload

hotreload({
  port: number /** Websocket port - default is 8080 */
})

// and 
exphbs.create({
  hotreload: true,
  port: number /** Websocket port - default is 8080 */
})

// or 
engine({
  hotreload: {
    port: number /** Websocket port - default is 8080 */
  }
})

engine

engine({
  hotreload: boolean /** Enable hot reload - default is false */
})

// or
engine({
  hotreload: {
    port: number /** Websocket port - default is 8080 */
  }
})

hotreload will actually return the port that it is using, so you can use that to pass to the engine function.

let port;
if (process.env.NODE_ENV !== 'production') port = hotreload();

app.engine('handlebars', engine({
  ...(process.env.NODE_ENV !== 'production') && {
    hotreload: {
      port
    }
  },
}));

Handlebars

The handlebars engine is the same as the express-handlebars engine.

Thanks

❤️ express-handlebars

Package Sidebar

Install

npm i express-handlebars-hotreload

Weekly Downloads

2

Version

0.0.6

License

MIT

Unpacked Size

30.9 kB

Total Files

7

Last publish

Collaborators

  • nickgraffis