The Best Tour companies in Top Egypt Day tours

Best Travel Agents Egypt is a country that tops the bucket-list country for many travelers. It’s a country stuffy with unreliable relics of ancient history, from the Pyramids and Great Sphinx of…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Express live reload everything

A guide on live-reload with express. Using Webpack for the public files and Nodemon and livereload for server files.

I have been working with Express.js for quite some time now. And the experience is rather cool. Sleek, minimalist, and powerful. You add what you want when you want it. And with tools like Nodemon, you don’t need to reload the server every time you make a change.

The problem starts when you build a full-stack site. You have to reload the browser every single time you make a change to your client-side files. And to that I say nay!

In this guide we are going to use Nodemon, live-reload, connect reload, and Webpack to implement live browser reload.

In this guide we are going to implement live browser reloading using Nodemon, live-reload, connect reload, and Webpack on Express.js.

But… can’t we use Webpack exclusively to handle all reloading?

Yes but…

Using only Webpack to refresh the browser has several cons.

For example. Let’s say that you are using a template engine like hbs. The problem is that these files are being rendered by the server.

What if we also process the template engine with Webpack?

That would solve one issue to introduce another. Express injects the variables automatically. If we use Webpack we lose that functionality.

My solution is to use a hybrid approach. I use:

Let’s get hands to work then but before…

Versions used in this tutorial:

Now let’s get to it.

I will be using Express Generator to generate the project.

Express Generator bootstraps for you an Express.js project with the basics set up. You can find more about it here:

In your terminal:

What is happening here?

After doing that, move to the project and install the dependencies.

Nodemon detects changes on your backend files and refreshes the server. This means that you don’t have to restart your Express server every time you edit your code.

Let’s install good ole Nodemon.

Then in the package.json use Nodemon with the scripts kind of like so:

Now you can run on the terminal:

Which will run Nodemon on the project.

We don’t only have server-side JavaScript in our Express.js site. We also have CSS, HTML, and SCSS. A lot of client-side files (I will only be doing js and CSS on this tutorial for the sake of simplicity).

We can use Webpack to handle all these files and generate something browser readable.

Let’s begin by installing it.

Now we need to configure Webpack. For that, we can use a file called webpack.config.js

We are going to add process JavaScript and CSS. To do that, we need Webpack loaders.

Loaders help Webpack load all the files you may need.

If you want to go deep into loaders take a look at:

To process JavaScript we need the babel-loader. Let’s install it.

To add support for CSS we need to install style-loader and css-loader like so:

Now we need to create the webpack.config.js file at the root of the project with this simple configuration

Let’s go line by line

2. It loads the principal file, the file that’s going to load the rest of the files.

3. Sets the configuration for the result of the build.

4. The mode of the build. Usually development or production.

5. This is where we set the loaders.

5.1. The test attribute matches the files’ extensions with a regex.

5.2. We use exclude when we want to “exclude” files that match the test attribute.

5.3 The loader attribute inside of use is where we define the loader we want to use for a specific file. You can use more than one loader for the same file. This is exactly what I do with use: [“style-loader”, “css-loader”]. Keep in mind that Webpack uses loaders from right to left.

We also need to define the index.js we were talking about in step 2. Here we are going to import our CSS and JS. The next two lines of code are enough.

Inside the app.js and app.css add anything you want the browser to run.

Finally, we are going to use Webpack thru a npm script.

The --watch flag tells Webpack to “watch” over the files that we want to rebuild every time we edit the code.

This is a very simple configuration for Webpack, very basic stuff. You may want to do a lot more with it. If you want to go into more detail check the docs in the link below:

Now we need to install livereload and connect-livereload.

These are the modules that are going to refresh the browser every time a change happens.

Basic configurations

In your app.js (this needs to be under your express declarations)

What’s happening here? Let’s see line by line.

2. Creating the livereload server.

3. Watches the public folder for Webpack’s bundles changes. When Webpack finishes the bundle’s construction we need to reload the browser.

4. Listens for Nodemon’s server reloads to refresh the browser.

5. The middleware that actually refreshes the browser

By default, Nodemon doesn’t watch .hbs files. We are refreshing the browser by listening to Nodemon’s server restarts. So we need to tell it to watch for .hbs files’ changes like so:

Since we are watching our public files with livereload. Nodemon should ignore the public folder.

At the root of your project create a file named nodemon.json. Add the following configuration to the file:

We have an Express.js setup that reloads everything. Which saves us time and helps keeps the F5 key intact.

A repo using these configurations:

Good luck friend and until the next adventure.

Add a comment

Related posts:

Why are Major Companies Shifting towards Tokenized Stocks?

Tokenized stocks or shares are a way to hold assets in a token form or digitally. This is not only cheaper to issue than the traditional method but also easy to transfer. This reduces the barrier to…

Suicides Upset Me

I watched Glee. I enjoyed it. I laughed. I cried. I sang along. Much drama happened behind the scenes, I guess by what I’ve read, and then there’s what’s going on with the actors’ lives. Mark Salling…

BREAKING THE SILENCE

Elancharan is a multidisciplinary artist and poet. He resides in Singapore with his family and cat, Leo. He has a strange love for all things poetical and Sci-Fi. A winner of the Montblanc X Esquire…