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

Setting up EJS as our Layout system

Setting up a backend server is a critical first step in creating an isomorphic React application. This tutorial walks through creating an Express.js server, configuring it to serve static files, and preparing it for React integration.

What is Express.js?

Express.js is a lightweight and widely-used Node.js framework that simplifies building web servers. With its intuitive API, you can quickly create robust server-side applications. It also integrates seamlessly with Webpack for efficient development workflows.

Steps to Build the Server

  1. Install Express:
    Use npm install express to add Express.js to your project.
  2. Create the Server:
    • Create a server.js file.
    • Import Express and initialize it using const app = express();.
  3. Serve Static Files:
    • Use app.use(express.static('public')) to serve files from a public directory.
  4. Start Listening:
    • Start the server with app.listen(3000) to listen on port 3000.

Why Avoid Transpiling on the Server?

In this setup, we avoid using Babel or other transpilers to preprocess ES6+ code for the server. By writing native JavaScript supported by Node.js, we ensure better runtime optimization and fewer dependencies.

Next Steps

Once the server is set up, we’ll expand its functionality by adding templating systems and integrating React for server-side rendering. This foundational work ensures a scalable and efficient architecture.

Ready to enhance your skills? Dive deeper with the full course.

2024

As of 2024, the landscape of server-side rendering (SSR) in React applications has evolved significantly. While setting up an Express server for SSR with React was a common approach, modern frameworks like Next.js have streamlined this process, offering enhanced features and improved developer experience.

Changes in Setting Up Server-Side Rendering with React:

  1. Framework Adoption: Next.js has become the preferred choice for server-side rendering in React applications. It provides built-in SSR capabilities, automatic code splitting, and optimized performance out of the box, reducing the need for manual configuration.

  2. Simplified Configuration: Next.js abstracts much of the complexity involved in setting up SSR. Developers no longer need to manually configure servers with Express or handle routing and code splitting, as Next.js manages these aspects efficiently.

  3. Enhanced Performance: With features like static site generation (SSG) and incremental static regeneration (ISR), Next.js delivers faster load times and improved SEO, making it a robust solution for modern web applications.

Next.js as the Go-To Server-Side React Framework:

Next.js has gained widespread adoption due to its comprehensive feature set and ease of use. It simplifies the development process by handling SSR, routing, and static generation seamlessly. The framework's ability to deliver high-performance applications with minimal configuration has made it the go-to choice for developers aiming to build scalable and efficient React applications.

In summary, while traditional methods involving Express and manual SSR setups are still viable, Next.js offers a more streamlined and efficient approach, aligning with modern development practices and performance expectations in 2024.

Video Transcript

00:01In the last lecture, we started setting up our server
00:03and now we need to take our HTML and not have HTML anymore
00:08'cause we wanna build it out on the server.
00:09To do that, we're gonna use a templating system
00:11that is called EJS.
00:13Through it, we're gonna inject our react data into that HTML
00:17that's inside of a template
00:19and enable us to create reusable template components,
00:22a lot more ably for static components,
00:25static information like our header information,
00:27maybe footer information, and so on and so forth.
00:30So by the end of this lecture, we'll have
00:32that integrated into our Express JS application.
00:36Alright, so the next step in our process.
00:38Right now we're working with basically static HTML,
00:42but in reality, what our goal is to move our react
00:46to run on the server.
00:47So to do that I'm gonna go into our folder
00:49and I'm just literally gonna go into our public folder
00:52and just delete our index.
00:54We don't need it anymore.
00:55So now that we don't have an index and we have the CSS files
00:58and the images, all that is still good.
01:00Oh, you know, let me undo that
01:01because I actually need this file.
01:03But I'm gonna go ahead and I'm gonna create a new directory
01:06here and I'm gonna call this file directory views.
01:11And I'm just gonna go ahead
01:12and grab our file, our HTML file for now.
01:16Just for the sake, just for this moment, I'm gonna take
01:19that file and just take it out of our public folder.
01:21We're gonna move it into views
01:23and we're gonna do some with thing,
01:24something with it in a minute.
01:26Now that we don't have the file inside of our public folder,
01:29we wanna basically render things out dynamically using
01:32a templating system.
01:34Now to be able to use a templating system, I'm gonna have
01:36to install another, something to append into our server.
01:40So to do that, I'm, again, I'm going to, I'm going
01:42to clear out, out my server break out of it,
01:44and I'm gonna go ahead
01:45and install a template engine, which is called EJS.
01:50Now there's a lot of different template engines,
01:52but we're just gonna work with that one
01:54and I'm gonna install it
01:55and we need it for our production environment.
01:57And I'm gonna install it.
01:58It's called EJS as it's installing.
02:01Let me continue and set it up.
02:03So, step number one, I'm gonna go into my application
02:05and at any point I'm gonna, I, I'm using the public folder.
02:09Great. So the first step, it's gonna check
02:11to see if something's there after I check
02:13to see if something is there,
02:15then we're gonna do the next operation.
02:17So in this case, I'm gonna set up our view engine.
02:21Basically I'm telling Express to use EJS
02:26to render things out.
02:27So now that I'm using EJS,
02:30whenever we call the render method,
02:31which we'll take a look at that in a, in a millisecond,
02:34the next step, I'm gonna go into our view itself
02:38and start creating it.
02:40So the view itself in generally speaking,
02:42you want to be organized.
02:43So I'm gonna go ahead
02:44and just create here in, in, in my view itself,
02:48I'm just gonna views, I'm gonna go ahead
02:50and create a new folder and I'm gonna call it layout.
02:53And I'm gonna go into my index file
02:55and I'm just gonna just push it right
02:57inside there. And I'm gonna go
02:58Ahead there and change our index
03:01to be something more generic.
03:03I'm gonna call it template or let's call it blank.
03:08Do ejs. And I'm gonna use e js.
03:12That's all good. And lemme just go ahead
03:14and close that index file.
03:15We're not going to use that file.
03:18And now that I have my, my file sitting there inside of
03:21that view, inside of that layout and layout,
03:23and it's called blank dot egs, let's go ahead
03:26and load that up into our application.
03:28And let's, let me show you the steps involved with that.
03:31So I'm gonna go into my server
03:33and in my server itself, what I wanna do is I wanna listen
03:36to a specific something.
03:39There's a lot of a lot of things
03:40that could happen on a server, but in my case, I want
03:43to go ahead and ask my route,
03:45and I'm literally gonna say it when a user reaches a certain
03:47destination in this case,
03:48when they reach the route destination,
03:50then I'm gonna go ahead
03:51and I'm gonna just get that respo, I'm gonna, I'm going
03:56to send that information.
03:57There's get and there's posts and there's other,
03:59but we're not gonna go deep into that world.
04:01But instead of that, I'm just gonna go ahead
04:03and create a get, which basically means there's no form
04:05that has been submitted in this case.
04:07Well, technically there could be a get form,
04:09but in our case we're really just loading up, uh, element.
04:12Now what's going on here is whenever a get happens,
04:15whenever the user gets a page
04:17or they're not posting something, they're getting a page.
04:19Then this callback will be called,
04:21this callback is gonna have the request information
04:24of the user and it's gonna have the response object
04:26that we're gonna be using to respond back to the user.
04:29And in our case, we really just want
04:31to do one very simple thing.
04:33We want to go ahead and we wanna respond with our
04:37render of what, of a specific layout.
04:41Now notice how I'm not gonna type your views.
04:43That is the default in Express
04:45and EJS, it's gonna look for it inside of the views folder.
04:49So I'm just gonna go ahead and set our layout
04:51and e well, in our case, I called it blank,
04:54but probably empty would be more appropriate.
04:58And in our case layout blank, it's gonna go ahead
05:00and call that blank, that EJS file.
05:03And I'm not gonna send a second parameter,
05:05but we will in the next lecture.
05:07And there we go. This will be our first rendering.
05:09And if I go ahead and say this, just running
05:11through this really quickly again, step number one,
05:14we installed EJS.
05:15Step number two, we went to express
05:17and told express use EJS as your template engine.
05:21Whenever we call the render next step, we went ahead
05:24and defined the path When the user goes to this route,
05:27in this case that means the route directory.
05:29And in that case, we don't want it on a post,
05:32we want it only when the user is getting the page,
05:34not when they're posting something or deleting something.
05:37In that case, when they're getting the page,
05:39we're gonna get, we're gonna use a arrow function,
05:41which it's first peram that will be sent to us
05:43as the either request information
05:45where we could gather information from the user.
05:48And the next is the response.
05:49And with the response we respond back to the user
05:52by using the dot render method,
05:54which then links back with our
05:56Our EJS templating engine.
05:59And we're literally telling it this is the file that we want
06:02to send back now if everything worked according to planned,
06:05if I go ahead and I run my server again and,
06:10and I run my server again,
06:12and once my server is running, if I go back here
06:14and I click here on refresh,
06:16we just still see the low world.
06:17But this time around it's coming from our templating engine.
06:21Now if you're wondering what's
06:22so great about using a template engine
06:24and not just static HTML, well that is part of our process
06:27because we want to inject inside of your information
06:30that is not static, which will start doing in
06:32the next lecture.
06:35In this lecture we integrated EJS into our express
06:38application and we're ready
06:40to move in the next lecture into actually integrating ES six
06:44and JSS support onto the server
06:46so we could start rendering React applications.

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!