Using Browsersync With Any Server and Custom File Watcher Without Proxy
Learn how to install this free JavaScript library, and start optimizing the performance of your web application of site. Read on for more!
Join the DZone community and get the full member experience.
Join For FreeBrowser-sync is one of the most used libraries for live reloading of a web page. It helps the developer a lot. If you want to know more about browse-sync visit this page.
Browser sync by default uses a node server to host your file.
I am going to show you how to setup browser sync with any server and custom file watcher.
Pre-requisites :
Node.js and npm.
Installing browser-sync:
Use the following command to install browser-sync:
npm install -g browser-sync
Starting Browser-sync:
Use the following command to start browser-sync:
browser-sync start
Adding browser-sync-client.js:
Now add the following code inside the <body> tag of all your HTML file.
<script id="__bs_script__">//<![CDATA[
document.write("<script async src='http://HOST:3000/browser-sync/browser-sync-client.js?v=2.18.12'><\/script>".replace("HOST", location.hostname));
//]]></script>
Almost Done:
Now access your web application through any browser. You will get the notification 'Browser-Sync connected' on your web page.
Now open the command prompt and enter the following command:
browser-sync reload
Now the magic happens, your web page will automatically reload in your server.
That's it, you are done. You can find many blogs for writing file watchers in any programming language. The only thing you need to do is to execute the command browser-sync reload
from your file watcher program.
And always, thanks for reading.
Opinions expressed by DZone contributors are their own.
Comments