02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Adding ES6/JSX Support on the Server

Setting up EJS as Your Layout System

In this tutorial, we transition from static HTML to a more dynamic server-side approach using EJS, a templating engine. By the end of this guide, you'll have your Express server configured to render reusable templates and integrate React components on the server.


Step 1: Move HTML to the Views Folder

  1. Organize your project structure:

    • Create a folder named views in your project root.
    • Move your static index.html file into the views folder.
  2. Rename the file to blank.ejs to reflect its new role as a reusable template.


Step 2: Install EJS

Run the following command to add EJS as a dependency:

npm install ejs --save

This step ensures EJS is available in your production environment.


Step 3: Configure Express to Use EJS

Update your server.js to integrate EJS:

// Load Express and initialize the app
const express = require('express');
const app = express();

// Set EJS as the templating engine
app.set('view engine', 'ejs');

// Serve static files from the "public" folder
app.use(express.static('public'));

// Define a route to render the EJS template
app.get('/', (req, res) => {
  res.render('layout/blank');
});

// Start the server
app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Step 4: Render the Template Dynamically

Now, when you navigate to http://localhost:3000, the server dynamically serves the blank.ejs file using the templating engine.


Why Use EJS?

EJS allows you to inject dynamic data, making it ideal for rendering server-side React components in future steps. This setup is critical for creating reusable layouts and integrating React into the server environment.

Ready to move forward? In the next video, we'll configure ES6 and JSX on the server to start rendering React components directly within this templating system.

Changes in 2024 and Modern Alternatives to EJS

As of 2024, EJS remains a straightforward and reliable templating engine for small or legacy projects. However, modern development practices often favor full-stack frameworks like Next.js or Remix, which integrate server-side rendering (SSR) and dynamic data handling natively without requiring a separate templating engine like EJS.

Key Updates:

  1. Component-Based Architectures: React developers increasingly use component-driven rendering, even on the server side. Frameworks like Next.js replace traditional template engines by dynamically rendering React components directly.

  2. Server Components: React's server components (introduced as a stable feature) provide a seamless way to fetch and render data on the server, reducing the need for intermediary templating layers like EJS.

  3. File-Based Routing: Next.js simplifies server rendering with a file-based routing system, negating the need for manual routing configurations common in Express + EJS setups.

  4. TypeScript Support: Modern projects often prioritize TypeScript for type safety. While EJS lacks TypeScript-first tooling, frameworks like Next.js fully embrace TypeScript.

  5. Static and Dynamic Rendering: Next.js offers a mix of static site generation (SSG), incremental static regeneration (ISR), and SSR, allowing developers to optimize for both performance and interactivity.

Summary of Using EJS in 2024:

  • Pros:
    • Lightweight and easy to integrate for small-scale applications.
    • Minimal overhead compared to full-stack frameworks.
  • Cons:
    • Limited scalability for larger projects.
    • Lack of advanced features like automatic code-splitting and prefetching.

Transition to Modern Tools: While EJS is still useful for educational purposes and small-scale applications, developers working on modern React projects should consider using Next.js. Its built-in SSR, API routes, and support for React's latest features make it a go-to choice for scalable, performant applications.

Next Steps:

For those continuing with EJS for templating, the next lecture will focus on integrating ES6 and JSX to bring React's power to the server. However, for production-scale applications in 2024, exploring Next.js for server-side React is highly recommended.

Video Transcript

00:00In the last lecture, we added support for EJS
00:03or templating in our express server.
00:06And in this one we're gonna show you how to integrate babble
00:09to have support for ES six
00:11and JSX in the most eff, eff effective way
00:14for a production type environment.
00:16And with that said, we're gonna go ahead
00:18and show you, we're gonna show you how
00:20to pass Oso properties into EJS
00:22and that will conclude our preparation
00:24before we could start actually rendering
00:26things on the server.
00:28So, as promised in the last lecture, we really need
00:31to make the content here dynamic,
00:32which right now is just static HTML.
00:35So just for the sake of this title, let's just go ahead here
00:38and I'm just gonna cut this out, we'll use that in a minute.
00:42And I'm gonna go ahead and put here the dynamic nature
00:45of this, the templating.
00:48Now literally what I'm,
00:49what I've just done here is I've wrapped with all these
00:52interesting markings and set up
00:53and said, Hey, by the way, a title needs to be here.
00:57Let's go ahead and also to set up our body.
00:59So I'm just gonna go ahead here and delete that a low world
01:02and replace that again with that uh, ruling
01:04that we've just met.
01:07And inside here I'm just gonna define a variable name
01:09and lemme lemme just put your space so it's nice and clean.
01:12And I'm gonna go ahead here and set here our body as well.
01:15So that's it, that's all I had to do. It's that simple.
01:18With EJS, I just put in all the dynamic p parts
01:21of our application.
01:22All that's left for me to do is to go back into my server
01:25where I'm rendering
01:26and send in a second parameter, which would be an object.
01:29And I would list out those parameters.
01:31So for our title, we're not mastering React user interfaces,
01:35but we're mastering react performance
01:41and for our body.
01:43Let's go ahead and set here a body.
01:48I got a body. Beautiful copy.
01:52Alright, so let's go ahead
01:53and we're gonna have to restart our server on contrary to
01:56how we were working previously inside of our application,
01:59um, what we're basically trying
02:01to do in our next element here is title is not defined.
02:04Why is it not defined? Because
02:05that's happening in the server itself.
02:07Our EGS template has been updated
02:09but our server has not been updated.
02:12So I have to go ahead and stop the server
02:14and restart it again.
02:15So lemme just go ahead and do that.
02:17And now that I'm restarting my server,
02:19if I go right back in there
02:20and click here on refresh, you should see
02:21that now we should have our mastering React performance
02:24and I got a body so beautiful we're already, uh, miles ahead
02:28and all it's left for us
02:29to do is literally start building out our server,
02:33our rendering of React.
02:35So let me show you the steps,
02:37the core basic steps involved with that.
02:39So the first core basic steps,
02:41I'm just gonna go ahead into the server itself
02:43and I'm gonna add a few more constants.
02:45So I'm gonna go ahead all the way on the top here
02:48and I'm gonna go ahead and create a new
02:50constant, I'll call it react.
02:51Just like, well, very similarly
02:54to the way we were working within uh, ES six,
02:56I'm just gonna use our, uh,
02:59Our node modeling way React.
03:02And I'm also gonna load up our React dom only.
03:06We're not gonna use React dom, we're gonna use React,
03:09react dom server.
03:11And I'm gonna go ahead and require our React
03:17do server.
03:18Okay? We don't need to install another there something.
03:20We already have the React packaging.
03:23Now, last but not least, okay, we set up our express server.
03:27It's running, uh, we have our content here running,
03:30but we do have one more step
03:31or two more steps really as one is to render out
03:34that application and I'm gonna go ahead and render it out
03:37and only load it up when it's actually needed.
03:40So I'm gonna, let me just clean things up here.
03:42Let's just push everything here to the back here.
03:44And you know what, let me even, let's go ahead
03:48and just, I'm trying to figure out a nice organizing
03:51because we don't want everything to be pushed up.
03:53Okay? That looks much better to me.
03:55Alright, so now that we've done that, I want
03:57to go into our rendering method here
03:59and I wanna show you a few steps.
04:00And we're gonna start off by on contrary by the way
04:03to E ES six where you could only import things on the lines
04:07above in the beginning, if you try
04:08to import stuff in the body itself, it would not work.
04:11But in express, in node using, um, cr um,
04:17create Js, you could literally load things up on demand.
04:22But I'm gonna go ahead and first start it up.
04:23So I'm gonna go ahead and just create a new constant
04:26and I'm gonna call this constant our app.
04:28'cause really that's what it's gonna be.
04:30It's gonna be the app and it's kind of confusing
04:32with the names here, that's why I'm putting it separately.
04:34And I'm gonna go ahead and require
04:37our current directory app.
04:40Now the problem with doing this is
04:42that our app itself is actually running in E es six.
04:47So our our, and let me just uh, fix that obviously.
04:49And our application is running, our app itself
04:53that we're loading here is an ES six item.
04:55And if it's an e es six item, um,
04:57moreover, so it's just not gonna work,
04:59it's not gonna render, right?
05:01It has JSX in there.
05:02I dunno if we're gonna right away see an error,
05:05but lemme just go ahead and try to run this again.
05:07So let's start our server up
05:09and when we start our server up, okay, everything is fine.
05:11If I start up and go to that specific item, we'll see, oh,
05:14oh, we have all these issues.
05:16Why do we have all these issues?
05:17Well, technically speaking app is not supported.
05:21Now we have a way to solve it
05:22and very easily we could solve it.
05:24And on contrary to the way we did it, uh, so far,
05:29I wanna go ahead and solve the problem.
05:31Now let me just go ahead and clear this out.
05:33When we were working with Webpac, Webpac packaged our files
05:36and compiled them down all the way in the
05:39beginning, in the start.
05:41On contrary, you could do it, we could do it in another way.
05:43And I'm gonna go ahead to NPM and install and save this
05:46'cause we're gonna need this for the runtime.
05:48I'm gonna install Babel Register.
05:50Now, Babbel register contrary to the way that it,
05:53that things were running previously. Babbel
05:57Register works this way.
05:58I basically require it once in my application,
06:01ideally in the root of the re application.
06:05And once I've done
06:06that every single time a file is loaded in.
06:08That's not one of the built in files such as one
06:11of the files that we're creating here.
06:13It will go through and transcribe it down.
06:15If it's a JavaScript file, it will transcribe it down.
06:18So it will be ES five
06:20and it won't have, well in our case it's not, it it,
06:23it will transcribe it based on whatever presets we had.
06:26So in our case, it's gonna transcribe the 2015,
06:28it's gonna transcribe the React stuff.
06:30So it's gonna make sure that we won't get any JSX errors.
06:33We're not gonna get any E es six errors of things
06:35that are not supported.
06:37Now the beauty of this is
06:39that it's only gonna do this when it's needed
06:42and then it's gonna cache it so that way if someone tries
06:44to fetch it, if we try to fetch this item again
06:47after the first user fetched it, the second, third, fourth,
06:49we'll all receive the same transcribed file
06:52that has been created and cached.
06:54So you don't have to worry about performance much.
06:56You don't have to pre-con compile everything.
06:58And the beauty about this is that we could continue working
07:01and actually using E ES six as long as our core file inside
07:04of here is not using ES six,
07:06but every other file beyond that could be an ES six.
07:09Now I'm gonna show you also in a lecture, next lecture,
07:12really technically speaking or the one
07:14after that, how could we also make this file
07:16to run an E ES six if we chose to do so?
07:20So with that said, let me go ahead
07:22and continue down this path of actually setting up our React
07:25to work, which we'll do in the next lecture.
07:29In this lecture we concluded getting all
07:31of our pieces together
07:33before we could have actually start rendering React elements
07:36on the server, which is exactly
07:37what we're gonna do in the next lecture.

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!