NodeJS rendering method for HTML pages

We are in the process of developing a fully functional social networking website that will resemble popular platforms like Facebook or Instagram. Our plan is to utilize Node.js on the server side and we are currently exploring the best technology for rendering view components on the server.

One major requirement for us is SEO friendliness, which makes Angular less suitable (although we are open to hearing arguments in favor of using Prerender.io).

Our goal is to incorporate AJAX support so that most of the rendering can be handled on the server while ensuring updates are carried out on the client side.

Although React appears to be a promising choice after our evaluation, there is one concern lingering - the default approach of loading all data at the route level instead of at the component level due to the synchronous nature of renderToString (as discussed here).

If we opt not to go with React, I am uncertain about a reliable alternative for server side rendering.

From my understanding, a potential straightforward method (which allows for async loading from sub components) could look something like this:

// Main page route
app.get('/',function(){
  var html = renderBase();
  renderHeader(html)
    .then(
    renderFeed(html)
  ).then(
    renderFooter(html)
  );
})

renderBase = function(){
  return = "<html><body>..."
}

renderHeader = function(){

  someIoFunction(function(){

    // Build HTML based on data

  })

}

It seems like employing a template engine such as Jade might ease some of the challenges, but I haven't come across much discussion on the topic of "React vs. Templating Engine," suggesting that I may be missing something important.

Thank you.

Answer №1

To tackle this issue, one approach is to establish a standard practice where each element contains a fixed function that retrieves the necessary information, and then invokes the same-named function in other interdependent elements. A simplified representation of this concept can be outlined as follows:

class PostList {
  static fetchData = (props) => {
    return {
      posts: api.getPosts({category: props.category})
    };
  }
}

class PostApp {
  static fetchData = (props) => {
    return {
      user: api.getCurrentUser(),
      contents: PostList.fetchData({category: props.postCategory})
    };
  }

  render(){
    return <PostList posts={this.props.contents.posts} />
  }
}

Alternatively, you could decide all data requirements at the root level, but while this seems more straightforward initially, it may lead to increased susceptibility to errors. This resembles the usage of templates.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Encountering difficulty in importing the graphql node package with the import keyword

Could you please clarify this for me? Whenever I attempt to bring in the graphql node package using the import keyword, the imported module is recognized as undefined. However, if I utilize require, the module imports correctly. Interestingly, other node ...

When utilizing req.query.property, an error occurs stating that the limit needs to be defined as a numerical value

Currently, I am working on a Mongoose/MongoDB query using the .aggregate method with $limit in the pipeline. When I hard code a number such as 2, everything runs smoothly without any issues. Similarly, if I assign a value to a variable like testNum = 2 and ...

Explore the capabilities of Vue.js by creating multiple JavaScript classes within your application

<div class="container" :class="{ qwerty: !open }" :class="lower? 'left' : 'right'"> Hey there, I've noticed that Vue seems to only allow me to add one class with conditions, as shown in the exam ...

Arranging two <ul> elements within an angular template

I need assistance with aligning two ul blocks. Currently, they are displaying next to each other, but their alignment is starting from the bottom instead of the top: <div class="ingredients-container"> <div style="display: inline-block; width: ...

Getting an error when trying to npm install jsbin

Attempting to set up jsbin. Currently working on it with jsbin. Encountering the following error. I am carrying out the process in a Linux environment, using the command: sudo npm install -g jsbin Encountered this error message npm WARN deprecate ...

Create an AngularJS task manager that saves your to-do list even when the page is refreshed

Currently, I am developing a straightforward to-do list application using AngularJS within an ASP.NET MVC template. Surprisingly, I have successfully integrated Angular and Bootstrap to achieve the desired functionality. The app allows users to add and re ...

Is there a way to select and handle multiple elements using async wdio with the same selector?

My test scenario involves using async wdio to test my React app, where I need to count elements with a specific selector and check their contents. The code snippet being tested is as follows: <div className='container'> <li className= ...

Ways to verify if a string is a number without using isNaN and with specified criteria

I am trying to determine if a string represents a valid number without relying on the isNaN function. The reason for this is that I need to be able to accept the ',' character, which isNaN ignores. Additionally, I do not want to allow negative nu ...

Experience seamless page transitions with React Router v4's sleek animation that smoothly guides you

I implemented a fade-in fade-out transition for routed components using RR4 and ReactCSSTransitionGroup, based on the example provided in https://reacttraining.com/react-router/web/example/animated-transitions While the animations are working, I am facing ...

Why do I need to specify the provider before the configuration in Angular?

Can someone explain why it is necessary to declare the provider before the config function that will use it? For example, this code functions correctly: angular.module('app') .provider('testService', function() { // ... }) . ...

Display outcomes according to whether or not they possess a specific attribute

When I query all users in my database, I receive two types of responses. { _id: "5bcf4f4d48a3067897c22344", __v: 0 }, { _id: "5bcf507b65f77778ab23b53f", firstname: "major", lastname: "general", __v: 0 } One response lacks the firstname and lastname prope ...

Diagnosing issues with the socket.io event system in a Node.js backend and Angular frontend

To establish a basic event system within my Node backend for notifying clients (Angular application) of specific events (such as when a user changes their profile photo), I am utilizing the socket.io library in the backend and its corresponding 'ngx-s ...

Error message for Components excluding TextField and SelectField

I am currently in the process of creating a basic form that includes various fields for data collection. One challenge I am facing is implementing validation for specific fields like textfields, checkboxes, and radio buttons. According to the documentatio ...

Incorporating the fadeout technique in conjunction with the append() function

Here is some code that I am working with: $('body').on('click' , function(){ $('body').append('<div class="ajax-success"><p>Its Done !!!</p></div>').fadeOut(2000); }); The goal was to a ...

Why won't Node.js let me redirect to my error page?

I've been putting together my newsletter project with the Mailchimp API, everything seems to be working fine except for when I try to redirect to a failure page if the status code is not 200. The browser shows an error message saying 'localhost r ...

How can we maintain the default menu open/close functionality in React-Select while also implementing a feature to open the menu on button click?

In my implementation, I am using a react-select component to search on an API alongside a button to initiate the search process. After clicking the button, I attempted to set menuIsOpen={true}. However, this caused issues as the original focus blur behavi ...

Believing that members possess a certain role when they actually do not

const { Discord, MessageEmbed } = require("discord.js"); module.exports = { name: "ban", description: "bans user from guild", execute(client, message, cmd, args, Discord) { const member = message.mentions.u ...

What are the steps to effectively create a cascade of Q promises?

Consider the following scenario as an illustration: I have 3 URLs stored in an array called "urls" The "require" function returns a promise that performs an $http call The code provided is functional, but it doesn't meet my desired outcome since th ...

Using innerHTML in React to remove child nodes Tutorial

I'm encountering slow performance when unmounting over 30,000 components on a page using conditional rendering triggered by a button click. This process takes around 10+ seconds and causes the page to hang. Interestingly, setting the parent container& ...

What is the best way to retrieve an array of objects that have a property matching another array?

In my array, I have data structured like this: array = [ { name: "john", tag: ["tag1", "tag2"] }, { name: "doe", tag: ["tag2"] }, { name: "jane", tag: ["tag2", "tag3"] } ]; My goal is to create a new array of objects that only contain elements with ...