Switch app engines in real-time based on the URL path with express framework

How can I dynamically set App Engine based on the URL?

In my application, I have two render engines available: serverSideRenderEngine & browserRenderEngine

If the URL is /home, the app.engine should be set as serverSideRenderEngine

If the URL is /login, the app.engine should be set as browserRenderEngine

Currently, for all URLs, I am setting my app.engine to browserRenderEngine

Below is the code snippet for setting up the app engine:

app.set('view engine', 'html');
app.set('views', distFolder);
app.engine('html', browserRenderEngine) // it is set to browserRenderEngine by default;

Is there a way to switch the engine based on the URL? Please provide assistance. Thank you.

Answer №1

If you want to customize rendering based on the URL, consider utilizing middleware such as:

app.use((req, res, next) => {
    // Check the URL
    if(req.originalUrl.startsWith('/home')) {
        app.engine('html', serverSideRenderEngine)
     }
    else {
        app.engine('html', browserRenderEngine)
    }
    next();
});

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

Error encountered while attempting to install material-ui v3.0.3 due to an unexpected termination of the JSON input

I'm currently in the process of installing the most recent stable version of material-ui(v3.03) by running: npm install @material-ui/core. However, I encountered an issue with npm ERR! Unexpected end of JSON input while parsing near '...-/brcast- ...

I have received a JSON multi-line string after entering information in a textarea

I've been working on a small social network project where users can create and share posts. However, I encountered an issue with formatting when storing and displaying the posts. When a user enters text with line breaks in the post creation form, it ...

How to eliminate undefined values from a dropdown selection in AngularJS

Blockquote When choosing a material from the column, the first option is showing as undefined. How can I remove undefined from the drop-down list? What changes need to be made in the HTML/JSON data for this to work properly? Blockquote var app = ang ...

Can you explain the distinction between cookies' maxAge and expiration date?

In my NodeJS Express app, I am utilizing cookies for certain features and need to set the cookie duration to one month. To achieve this, I have configured the cookie's maxAge property as days*hoursPerDay*minutesPerHour*secondsPerMinute*1000, which tr ...

Using the Parallax Effect in React

I'm currently working on implementing a parallax effect in React and I've encountered an error that's giving me trouble: Below is the snippet of JavaScript code I have for the parallax effect: export const parallax = document.getElementsBy ...

Which is quicker: reading two files with fs.readFile or using fs.readFileSync?

During the 5:40 mark of this video, it is mentioned that the non-blocking version (using fs.readFile instead of fs.readFileSync) reads files in parallel, resulting in faster execution. But how could this be possible if Node.js operates on a single thread? ...

Retrieving distinct items from an array containing nested objects and sub-properties

In my script, I am attempting to retrieve unique objects based on the state from an array of objects. The current script is functioning correctly for most cases. However, there is one scenario where a nested object in a specific state has a sub-state, and ...

Passing a deconstructed object as a parameter for a function

I'm having trouble understanding the parameter const Posts in the code snippet below. As a newcomer to node/React, I'm not sure if it's a destructured parameter object or just an object being passed as a parameter. The functions getPosts an ...

What is the best way to organize Node/Express routes based on their type into different files?

My /router/index.js file is becoming too cluttered, and I want to organize my routes by group (user routes, post routes, gear routes) into separate files within /router/routes/. Here's what I currently have set up: app.js var express = require(&apos ...

How can you restrict the number of characters a user can input into an Angular input textbox?

I am using the textarea tag and I would like to limit the number of characters a user can type to 300. Currently, I have implemented real-time character count functionality, but I need to restrict input once it reaches 300 characters. Below is my HTML cod ...

Tips for providing a base URL in Angular to fetch images from an API

How do I access the API URL to retrieve images from a database? <table class="table"> <tr> <th>Id</th> <th>Image</th> </tr> ...

Select from a list to save

My goal is to create a feature where users can select a hotel name, number of days, guests, and peak time, the system will calculate them together and give a sum. Furthermore, I wish to store all user selections in the database, including the calculated to ...

The puppeteer seems to be struggling to locate the element

Trying to use puppeteer to locate the login button on a task planner account, but encountering an error: "PS C:\Users\leticia.queiroz\Visual Studio> node Teste2.js (node:14920) UnhandledPromiseRejectionWarning: Error: No node found for se ...

Cloning a file input does not retain the uploaded file in the cloned input. Only the original input retains the uploaded file

Whenever I duplicate an input type file, I encounter an issue where the uploaded file from the duplicated input is also linked to the original input. It seems like the duplicate input is somehow connected to and taking files for the original one. This is ...

Personalizing the predefined title bar outline of the input text field

The outline color of the title in the input textbox appears differently in Google Chrome, and the bottom border line looks different as well. <input type="text" title="Please fill out this field."> https://i.stack.imgur.com/iJwPp.png To address th ...

Learn the best practices for integrating the options API with the Composition API in Vue3

Using vue3 and vite2 Below is a simple code snippet. The expected behavior is that when the button is clicked, the reactive 'msg' variable should change. It works as expected in development using Vite, but after building for production (Vi ...

Rest assured, with Ajax Security, your protection is in good

I am currently developing a browser game that heavily utilizes AJAX instead of page refreshes. The combination of PHP and JavaScript is being employed for this project. However, during the course of my work, I became aware of the potential security vulnera ...

Utilizing JavaScript to eliminate objects from a collection and showcase a refreshed arrangement

On a simple webpage using bootstrap and js/jquery, data is fetched via ajax to create entries displayed to the viewer. The process involves: - making a request - parsing XML to create an array of objects with all the data - sorting by one property - cr ...

Error encountered while trying to eliminate array element

I have a project in the works that involves creating a page where users can filter suggestions and vote for their favorites. The screen is divided into 3 tabs: [Top Rated], [Newest], and [My Votes]. Upon loading the screen, a call to the database is made ...

Guide to establishing a connection between Reactivesearch and an external Elasticsearch cluster

Trying to link my reactive search app with an external Elasticsearch provider (not AWS) has been a challenge. The provider does not allow any modifications to the Elasticsearch cluster or the use of Nginx in front of it. In accordance with the reactive se ...