Connect middleware for BrowserSync.
npm install connect-browser-syncConnect middleware for BrowserSync.
Use this middleware to automatically inject the necessary BrowserSync tags into your HTML pages. Alternatively, you can integrate BrowserSync with your app using Gulp or Grunt.



Assuming you will only use BrowserSync in development:
```
npm install browser-sync --save-dev
npm install connect-browser-sync --save-dev
`javascript
// app.js
var express = require('express');
var app = express();
// Other configuration...
if (app.get('env') === 'development') {
var browserSync = require('browser-sync');
var bs = browserSync.create().init({ logSnippet: false });
app.use(require('connect-browser-sync')(bs));
}
// Routes and handlers...
`
See the BrowserSync API docs for initialization options.
- You must use version 2.0.0 or greater of the browser-sync package.app.use
- The statement must come before any handlers that you want to integrate with BrowserSync. This includes both dynamic route handlers and static asset handlers.Content-Type
- Injection only happens on responses with a header of text/html and content containing a
or tag.Compatibility
$3
If you need to use BrowserSync 1.x, use version 1.0.2 of this package.
$3
Turbolinks and BrowserSync can conflict (see turbolinks#147 and browser-sync#977). As a workaround, you can inject the BrowserSync tags into
instead.To inject the tags into
, specify { injectHead: true }:
`javascript``
if (app.get('env') === 'development') {
var browserSync = require('browser-sync');
var bs = browserSync.create().init({ logSnippet: false });
app.use(require('connect-browser-sync')(bs, { injectHead: true }));
}
See the example folder.
Copyright © 2014 Chris Schmich
MIT License, See LICENSE for details.