02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous Video
Chapter is concluded.Next is MongoDB.

Back to React

Back to React: Integrating React with Express Server using Webpack

In this tutorial, we're going to revisit our React application and learn how to integrate it with our existing Express.js server. We'll also use Webpack to optimize our JavaScript and automate some processes, making it easier to run and maintain our code. The content of this tutorial is based on a video created in 2016, so I've added some comments for critical updates where necessary.

Overview

In this tutorial, we'll:

  1. Set up our React environment using Webpack.
  2. Configure Webpack to automate and optimize processes.
  3. Move from a browser-based Babel to a server-side Babel setup.
  4. Integrate React into our Express.js server.

Prerequisite: You should have completed the previous section where we built a basic server using Express.js.

Step 1: Setting Up the Project

Moving Files to a New Project Directory

To start, let's set up a new project directory for our React application:

  1. Create a new project folder for your React app.
  2. Copy the files from the Express.js folder (the project we worked on in the previous lecture) into this new folder.

Navigate to your terminal and stop the server from running if it is currently running.

cd [your_project_directory]
npm stop  # Stops the server if it's running

Step 2: Configuring Webpack

What is Webpack?

Webpack is a bundler that takes JavaScript files intended for the client and wraps them into one file, optimizing it for distribution. This also ensures that modern JavaScript features and JSX are translated into versions compatible with older browsers.

Creating a Webpack Configuration File

First, create a Webpack configuration file by running the following command:

touch webpack.config.js

Open the newly created file (webpack.config.js) and set up the following configuration:

const path = require('path');

module.exports = {
  entry: './src/app.jsx',
  output: {
    path: path.resolve(__dirname, 'public'),
    filename: 'app.js'
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env', '@babel/preset-react']
          }
        }
      }
    ]
  },
  mode: 'development'
};
  • Note: In 2024, it is recommended to use Webpack 5 for modern JavaScript applications. Update the dependencies to the latest versions to benefit from performance and security improvements.

Step 3: Installing Dependencies

We need to install the necessary dependencies for React, Babel, and Webpack. Run the following command to install the required packages:

npm install --save react react-dom
npm install --save-dev webpack webpack-cli babel-loader @babel/core @babel/preset-env @babel/preset-react

These packages will allow us to use React, translate JSX into JavaScript, and bundle our files.

Step 4: Configuring the JavaScript File

Moving JavaScript Files to a Private Folder

Move the main JavaScript file (app.js) from the public folder to a private folder (src). Rename it to app.jsx to indicate it is using JSX syntax:

mkdir src
mv public/app.js src/app.jsx

Update the app.jsx file to include the following imports:

import React from 'react';
import ReactDOM from 'react-dom';

// Your React code follows here
  • Note: Modern React versions use React Hooks, so consider updating any class components in the code to functional components with hooks for a cleaner and more efficient approach.

Step 5: Bundling the Files with Webpack

Now that our configuration is complete, let's bundle the JavaScript using Webpack.

npx webpack

This command will generate the final app.js file inside the public folder, which can be served to clients.

Automating with NPM Scripts

To simplify running the build process, add a script to package.json that runs Webpack for you. Open package.json and add the following script under the "scripts" section:

"scripts": {
  "start": "webpack && node index.js"
}

Now, you can start your server and build your bundle with:

npm start

Step 6: Testing the Application

Run the server and check if everything works as expected. If you navigate to your React app in a browser, you should see your login page or other React components properly rendered.

node index.js

If you run into any issues, make sure the JavaScript is properly loaded in the browser and there are no syntax errors in the bundled app.js file.

Summary

In this tutorial, we learned how to set up Webpack to automate the bundling process for React and Express.js integration. We also moved Babel from the browser to the server side, improving performance. This setup will allow you to work efficiently with modern JavaScript features while ensuring compatibility with older browsers.

Next, we'll be diving into MongoDB, exploring how to create database entries and make our app even more dynamic.

Video Transcript

00:00It's time for us to go back home to react.
00:03And in this lecture we're gonna learn about a few really
00:05important things that most full stack developers work with.
00:10One is we're gonna learn and get more familiar
00:12with a Webpack, which is a web packager that enables you
00:16to package up your applications
00:18before you send them to the actual server to run.
00:21And we're gonna see that in action in this lecture
00:24as we learn how to work
00:25with Babel on the server side instead
00:27of running it on the browser itself, making sure
00:30that our Babel doesn't need
00:31to run on the client side at all.
00:34Last but not least, of course we're gonna integrate it all
00:36with our React sample.
00:38So let's jump right into it.
00:41All righty, so it's time for us to get back
00:43and build out our React login.
00:46Now you already know how to work with Express JS
00:49and I went ahead and created a new project file.
00:51Just create a completely new project file
00:54and what I want you to do is just place the files from
00:57Express JS folder, that last one
00:59that we worked on just in the last lecture.
01:03Once you have that, you're ready to start our project.
01:06Go inside your terminal, make sure that you're inside
01:08of your project itself.
01:09Stop your server from running. Let me just clear our screen.
01:12We're inside of that project file now I'm gonna go ahead
01:16and install a lot of things
01:17and as it's installing, I wanna start working
01:20on configuring something, which is called Web Pack.
01:24Web Pack is basically a way for us to automate a process,
01:28which is a lot of processes.
01:30Basically what we're trying to do is we wanna
01:33automate the process of taking a lot of JavaScript files
01:36that are intended for the client
01:38and wrap them into one file.
01:40Also making sure that it's not using JSX
01:43and it's transcribed to be the version of the browser
01:47that we want it to run for Really moving it back
01:49and notching it down to Es ex five, ES five instead
01:53of ES six, and really to any version
01:55of JavaScript in the future as well.
01:58So using Webpac, we could ask it
02:00to basically bundle up a few operations
02:02and do those operation for us in advance.
02:05Now even before we start the installation, what I want
02:08to do is I'm gonna go ahead and touch a file,
02:10which is called web pack config js, just
02:14so we have that file.
02:16If I go back into my, um, editor area here,
02:19well into my folder you'll see
02:20that I have the web pack config.
02:22So lemme just open it up inside of our editor
02:24and I'm just gonna have already our package open
02:27and we have our index file open as well
02:30and we have your, our web back, okay, a nice
02:32and empty web so that way we'll could start working it right
02:35as soon as we start with our installations.
02:37So let's go ahead and start installing all the things
02:40that we're going to need.
02:41So I'm gonna go ahead into my NPM I'm gonna ask to install,
02:44I'm gonna make sure it's also saved
02:46because I'm gonna need all these saved
02:48and I'm gonna start with our web pack,
02:50which is gonna be basically what's the, the packager
02:54that's gonna package all our things
02:56and turn them into one file.
02:58The next thing that I want is I wanna run a react
03:01and configure uh, all of the JavaScript
03:03that we're gonna need right from Node itself.
03:06So to do that I'm gonna ask for React
03:09and I'm gonna re-ask for React dom to be installed, both
03:12of them enabling me to have the React
03:14and react dom just like we worked
03:16with on the client in our first section.
03:18The next things that I'm gonna need,
03:20and if we take just a a, a, well,
03:21we'll take a peek in a second.
03:23The next thing that I'm gonna need is I'm gonna need a few
03:26different packages of Babel.
03:27So Babel is that transcriber
03:29that actually translates our code from that version,
03:33whatever version into the current version
03:35or the version that we think will run on
03:36browsers that we wanna support.
03:38And to do that I need to install a few packages.
03:41One of them is Babel core,
03:42which is just gonna gimme the core functionalities
03:45of babble without really doing anything.
03:47The next thing is I want to add in a few presets
03:49and these presets are gonna tell Babel how to react
03:52or really what is it gonna do, what should, what type
03:55of JavaScript should it expect
03:57and what it needs to do with it.
03:59So I'm gonna go ahead and I'm gonna say basic.
04:00Basically Babel, you need to expect the preset of ES 2015.
04:06There's also another Babel preset that we want to tell it
04:09as preset react
04:11because we're gonna be working
04:12with React on the client side.
04:14Last but not least, we also wanna make sure
04:16that we have Babel loader
04:18because we're, we wanna make sure that the files
04:21that are loaded within that item will
04:23also be captured as well.
04:24Okay, so those are all the items that we want to install
04:27and I'm just gonna go ahead and click on enter
04:28and let it go through the process of going
04:30through all these installations.
04:33In the meantime, let's go back into our folder
04:35and what I wanna do in our project folder,
04:37let's go into our public folder.
04:39And what I want you to do in your public folder is go back
04:42and let's get our React, our final files for our React,
04:45which are all right here.
04:46And I'm just gonna go ahead and just copy all of them
04:49and move them into that folder.
04:50And I'm gonna replace our h our index file,
04:53which is completely fine.
04:54And lemme just also make sure that everything is arranged
04:57and let's just go ahead and
04:58delete the things that we don't need.
04:59So we don't need the login and register
05:01because it's all captured inside of our app js.
05:04We don't need our build
05:05because that's where that our reacts that's there,
05:08we just don't need it.
05:09So I'm just gonna go ahead and delete that as well.
05:12So all that we really need at this stage is our index file
05:15and our app js.
05:16Now if the next thing that I wanna do is I want
05:19to go into our editor
05:20and in our editor, let's go into our index file
05:22and uh, it doesn't seem like it reloaded it,
05:24so let me just not save that and go ahead
05:27and drag our index in here.
05:29So we have our index. First of all,
05:31we're not gonna need Babel anymore,
05:33we're not gonna need React anymore
05:35'cause we're loading it already through, we're loading it
05:38through our, um, node.
05:41So uh, our through NPM
05:43and also we don't need a special type for Babel,
05:45we're just gonna use a regular JavaScript.
05:48Now that's really all the changes we need to do in our HTML.
05:52Let's go ahead and see what changes we need to do now inside
05:54of our JavaScript file.
05:56And lemme just drag our JavaScript file in
05:58and I'm just gonna delete these
06:00comments 'cause we don't need them.
06:01Now our problem will be that we don't have access to react,
06:04nor do we have access to react dumb to give access to that.
06:08I'm gonna go ahead and set my React
06:10and ask to require just like we do in Node
06:13because this is a node request.
06:15So we're gonna require, require,
06:19we're gonna require react.
06:21Lemme make sure that that's a string.
06:24And last but not least, we also wanna require Dom.
06:27So let's go ahead and require re let me just copy this.
06:31Require n our installation is called React dom.
06:35So I'm placing that into our React dom
06:37and react making sure
06:38that it's gonna have access to these elements.
06:40Basically it's gonna automatically load those files
06:42and bundle 'em up because we're gonna use web pack.
06:46Okay, so now that we've done all the changes we want to do
06:48to our application to app js
06:50and our index dot html, it's time for us
06:53to take a look at our webpac dot config.
06:56Now before I do that, I just wanna make sure
06:57and let me just uh, double, triple check
07:00that our files com finished loading
07:02and they've finished loading.
07:04Let me just go ahead and I wanna show you, um, just
07:06for a second inside of our um, package that j json here,
07:12I wanna make make sure that you have a second to catch,
07:15capture all these dependencies.
07:17Now as we're going continue on with our development.
07:19As you remember, we talked about
07:21that in chapter two when we talked about NPM in general.
07:25Different versions, versions with our SEM VAR video.
07:27Different versions
07:29of applications could break our application.
07:31So if anything doesn't work, make sure that you go
07:34through your dependencies
07:35and ask to install these specific versions of the files
07:39that we're working with, okay, to make sure
07:41that everything continues working.
07:43With that said, let's go ahead
07:45and start building our web pack.
07:47And to do that what I want to do is I want
07:49to go back into our app
07:50and really what I want to do is I wanna make sure
07:53that our app js,
07:55and I'm just gonna copy it,
07:56is not gonna be in our public folder
07:58because that's, this is not the final file
08:00that we want to send to the browser.
08:01It has inside of it all these new features of JavaScript.
08:04Instead of that, I'm gonna put it into our private folder
08:07and lemme just go ahead here,
08:08I'll create here a React folder just so I know
08:10what are my react files
08:12and I'm gonna place that file right inside there.
08:15And just for the sake of clarity,
08:16I'm also gonna change the file name to JSX.
08:19So I know this is not files that need
08:21to go right into the browser.
08:22This is a JSX file
08:24and let me go out outta there into my public folder
08:27and delete our app js.
08:29What I wanna do next is I want to go inside
08:31with my webpac do config.
08:34I literally want to basically tell it a story
08:36and tell it where should it find the j the files that need
08:40to be changed and where should it place it.
08:42So let's go ahead and do that.
08:44And inside of here I'm gonna need, oops, see inside
08:47of here I'm gonna need a couple of things.
08:48The first thing I'm gonna need the web pack library
08:51or module more accurately.
08:53So I'm gonna ask for web pack
08:55and I'm gonna require, what am I gonna require?
08:58I'm gonna require a Webpack. Beautiful.
09:00So now that I have access to Webpack,
09:03I can now start doing uh, the things I really need to inside
09:06of my package here.
09:08So the next thing that I'm gonna do is I'm gonna go ahead
09:11and I'm gonna create a module.
09:13Now, the module itself we haven't really talked about
09:15and haven't seen ourselves until now.
09:18Until now. Our modules, what were they?
09:20They were stuff that we worked with that we loaded them,
09:22but we never created a module ourself.
09:25What we're gonna do right now is we're basically exporting
09:27our own module really
09:29and in this model we're just gonna export
09:32and don't worry too much about the terminology, as long
09:34as you make sure that you do everything that's
09:36inside of this video.
09:37And if anything here is a little bit too fast,
09:39then make sure that you go
09:41and just download the files from our source files.
09:44So basically what I'm doing here is I'm creating an export
09:46that is going to be visible to external files
09:50that wanna approach this file in no js.
09:53And what I want to do is I want to go ahead
09:55and in this exports I wanna define
09:57what is going to be exported.
09:59So the first thing I want to export is my entry
10:02and my entry is gonna be the starting file
10:04that is gonna start the process of the conversions.
10:07In this case I only have one file and I'm,
10:09and it's our starting file as well.
10:11So I'm gonna go into my private folder, I placed it inside
10:14of the React folder and I call the file app jsx.
10:18Okay, so we have our starting file.
10:20The next step is I wanted to find an output
10:23and for our output, I'm gonna go ahead
10:24and send two parameters.
10:26One is the path of our output
10:28and I'm gonna go ahead and set a folder.
10:30The reason is, is because in some scenarios
10:32and many scenarios you're gonna output more than one file.
10:35In this case we only have one file.
10:36So I'm setting my base folder
10:38and I'm also setting my file name
10:40and that file name is gonna be app js,
10:42which is gonna be the output file that I want to create
10:45because that is the file that is expected in our browser.
10:48It's expecting an app js.
10:50Okay, so now that we got that sorted out,
10:53let's move on to our next item.
10:55Our next item is we wanna make sure also that our experts go
10:59through some sort of a process of conversion.
11:01To do that, we're gonna have to load up some modules really
11:04and let it know what are the modules that it's going
11:07to use to do that.
11:08I'm gonna go inside here inside of our module area
11:11and I'm gonna ask for a loaders a loader.
11:13Well in this case there's loaders of ple
11:15but really only sending one loader.
11:17And in this loader what we want
11:19to do is we're gonna send in one object
11:21and this object is gonna contain basically all of our
11:25rules and what are we gonna load?
11:27So first of all, we have to decide
11:29what is gonna go through a conversion.
11:31And I'm gonna make sure that I'm gonna start off by saying
11:34that the test that we want to do is we want to basically ask
11:37for, and we have to send you a reg reg X expression.
11:40And this Reg RegX expression is literally just gonna be JSX
11:44question mark dollar.
11:46And what I'm actually doing here is I'm literally telling
11:48Jay I'm, I'm saying in in Reg XI only want files that uh,
11:53end with JSX, there are a di a file extension of JSX.
11:56Those are the files that I wanna work with.
11:59The next thing I want to go is I want to go
12:00to the loader itself and define what is the library
12:03that is actually gonna convert these files.
12:05In this case we have our Babel loader,
12:07which is the Babel library that is gonna load all the things
12:10that are needed to be able to change
12:12and configure are elements and turn them into that one file.
12:16The next thing that I want to do is I also wanna make sure
12:19that I'm gonna exclude,
12:21although in this case it's not that critical,
12:22but in generally speaking you could also exclude the files
12:25that you wanna make sure will
12:27or the folder that you want to make sure
12:28or the elements you wanna make sure never go
12:30through any conversion.
12:32And again, I'm gonna go ahead here
12:34and what I'm gonna do is what I'm gonna do is I'm going
12:36to make sure that I would ne I'm never going to
12:39actually convert anything that is a node module,
12:42making sure anything that's coming from
12:44that folder is gonna not change, not gonna convert.
12:47As I'm loading different things inside of my module,
12:50I wanna make sure that anything that is a node js
12:52module we're not gonna touch, not gonna change.
12:56Next on we're gonna make sure
12:57that we're gonna set here a query.
12:59And in our query itself we're gonna send parameters
13:01for this loader telling it what is it gonna actually do?
13:05So I wanna tell it in its presets.
13:07Remember that we loaded up, if you recall in our package
13:10that Jason, we loaded a few presets
13:12and what I want to say is tell that loader
13:14to use these two presets.
13:16So to do that I'm gonna go into my presets parameter
13:18and send inside an array that has a list of those items.
13:22So first of all, we're telling it that we're using ES 2015.
13:27What else are we telling it? We're also telling it
13:28that it's a React project and there we go.
13:32So we've just created our expert
13:33and this is really all the things we
13:35need to do inside of this package.
13:36All that's left for me to do is save it.
13:38Once it's saved, it's time for me to test out
13:40and see if everything is working.
13:42Now the way I do that and if I'm gonna go into my terminal
13:45and lemme just clear everything out here, what I want
13:48to do is I want to go into my node modules,
13:50into my bin folder and I want to ask for web pack
13:55and I could put it, uh,
13:56the basic two parameters are production or development.
13:59I'm gonna ask for development.
14:00Now what should happen in theory if we've set everything
14:03correctly, it should go to this file
14:06and it should basically use Baba Loader with the presets
14:10of ES 2015 and react and produce the file app js.
14:15Now I want you to also to notice another something
14:17that we are loading up React and react dom inside of here.
14:20And actually these external files are also gonna be bundled
14:23up and turned into one file.
14:25So let's go ahead and give it a shot
14:26and let's run it and see if it actually works.
14:28We're gonna choose the, the development option
14:31and as we run it in our development option,
14:33it's gonna think a little bit, it's gonna go
14:35through its process and it's gonna create that app,
14:37that JS file.
14:39So if we go inside of here into our public folder, we'll see
14:42that we have our app js.
14:44Now if I take this file and I take a peek at it,
14:46lemme see if it reloaded, it sure did.
14:48If I take a peek at it, you'll see
14:49that we now we have our massive file including both React,
14:53dom React and also our own original files.
14:55Somewhere inside of here you'll find it as well.
14:59Now when you do and you run it
15:00and you run it for production instead of development,
15:03what will happen, it's not gonna create this massive
15:05747 kilobyte file.
15:07Instead of that, when it's done processing,
15:10it'll create the file minified by the way,
15:12giving you warnings also about things
15:14that might not be perfect,
15:15but in our scenario we're gonna let it,
15:17let it run the way it is
15:18and you can see that now everything is all
15:20compressed and condensed.
15:22With that said, let's go ahead
15:23and continue on with our process and I'm gonna go ahead here
15:26and put it back into our development
15:29'cause we really are in development.
15:31We want a development version
15:33as it's going through this process.
15:34The last thing that's left for us
15:36to do is actually run our server
15:38to check if everything works.
15:39I'm gonna go our no node and ask no to start our index file.
15:44And if I go ahead there, our server should start running
15:47and if I go back into my application
15:49and click here on refresh, we should see our login run with
15:53React and O2.
15:55If we look at our JavaScript, it will not have any
15:57of our modern new JavaScript
15:59and if one, once it's inside of our browser.
16:01Now I wanna wrap things up with one more thing
16:04and just to show you something
16:05that will make your life really easy
16:07by automating some of this process.
16:09Now, after you've created all this, I just want you to go
16:12inside of your package, do JSON
16:14and remember that script folder
16:15that we've never really done anything with it.
16:17What I want you to do is I want you
16:19to add a new start option in your scripts.
16:22And there I want you to basically say web pack.
16:26Now, because we're inside of our scripts, we're actually
16:29inside of the world of that, um, of our node.
16:32So really in in this essence, in in essence we don't need
16:35to type module bin web pack
16:37because when we're inside scripts, we're inside of our bin.
16:39So I could literally just ask to do
16:41that web pack D And if I go ahead here,
16:44I'm just gonna say web pack D.
16:45But not only that, after that is done, go ahead
16:48and call Node and start our index
16:51literally starting our server.
16:53So if I go ahead here
16:54and just do one more time, clear everything up here,
16:57let's clear everything up and instead of doing, instead
16:59of calling my no js, I'm gonna go to NPM
17:03and call NPM start instead, which corresponds
17:06to the start inside of our script.
17:08Now when I do that NPM start
17:10and I click here on the release, it's gonna go first
17:12and start our web pack, package it up and start our server.
17:17So when I go back to my server,
17:18I'll have my server running with React.
17:21And there you go. We've just connected our react
17:23to our real live working server.
17:25This was a mouthful and a lot of things.
17:27So if anything here was a little bit confusing,
17:29make sure you go back to the file project
17:31and take a look at it, play around with it a bit
17:33before you continue onwards
17:34and also maybe rewatch this video 'cause it's very impactful
17:37and we're doing quite a lot of things inside here
17:41in this lecture we learned a lot of things such as Webpac,
17:45integrating React on your server and using
17:48Babel. Now
17:49we're not gonna continue building up this project
17:52because instead of that we're gonna go back
17:53to the core foundations and learn one more skill.
17:56You must know, by the way, by the end of knowing that skill,
17:59we're gonna start over our project learning even more things
18:02of things that you could do in the combination
18:05of all these technologies that we've learned.
18:07So next lecture we're gonna re new move to a new topic,
18:11which will be all about MongoDB
18:13and creating database entries.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!