The challenge of incorporating multiple stylesheets into an express/node/ejs application: encountering the error "Undefined style."

I am working on a project using express, node, and ejs and I want to be able to use different stylesheets for different views. In order to render a specific view with its corresponding style, I have included the following in my app.js file:

app.get('/mySite', (req, res) => {
res.render('mySite', { style: 'mySite' });

In this case, let's assume I have created a stylesheet named mySite.css and in my boilerplate.ejs file, I have added a link to the desired styles:

<link rel="stylesheet" type="text/css" href="/stylesheets/<%=style%>.css">

The challenge I am facing is how do I define 'style' without encountering an error saying "style is not defined"?

I apologize if this question pertains to basic knowledge in express, as I am still in the learning process.

Answer №1

When I was learning, I found success using a similar method by creating an object in my controller.js file with the names of the stylesheets.

const styles = {
  index: "styles",
  register: "register",
  login: "login",
};

I simply added the style attribute when rendering each page:

const mainController = {
  index: (req, res) => {
    res.render("./products/index", {
      showRoom: productCollection,
      style: styles.index,
    });
  },
  register: (req, res) => {
    res.render("./users/register", {
      style: styles.register,
    });
  },
  login: (req, res) => {
    res.render("./users/login", {
      style: styles.login,
    });
  },
};

This way, when rendering index, register, etc., it calls for the name of the stylesheet to complete the href in the HTML:

  <!-- APPLIES EACH SPECIFIC STYLESHEET TO EACH SITE -->
  <link rel="stylesheet" href="/styles/<%= style %>.css" />
  <!-- ------------------------------------------------- -->

Answer №2

Alright, I've discovered that styling can be achieved by referencing the appropriate route, but in the boilerplate code, an ejs syntax is required for styling references.

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

Troubleshooting: Azure Functions with NodeJS fails to implement Hot Reload functionality

I'm having trouble enabling hot-reload for my Azure Function app built with NodeJS. I've searched everywhere but can't seem to find any information on how to do this specifically for NodeJS, only for .Net Core. Can anyone point me in the rig ...

Step-by-step guide on mocking an asynchronous function in nodejs with jest

Within this function, I need to mock the httpGet function so that instead of calling the actual function and returning its value, it will call a simulated function fetchStudents: async(req,classId) => { let response = await httpGet(req); return r ...

Adjusting table font dynamically using Angular and CSS

I am currently working on an Angular project and facing a challenge with setting the font size dynamically in a table. I have created a function to address this issue, which includes the following code snippet: $('.td').css({ 'font-size ...

"Exploring the compatibility between Android browsers and Socket.IO technology

I am facing an issue with getting socket io to send a response upon connecting in the Android browser. Even though I can see the parameters logged on the server side, it appears that the client side connection is not working properly. I have disabled jsonp ...

Separate Camunda from the user interface

Having recently started using camunda, I am still learning about it. My current application is built with Nodejs and React, and I have successfully integrated camunda for server-side operations using the camunda REST API. In terms of my UI, it consists of ...

Struggling with color styling within link_to tags in Rails 4 - having trouble getting inline styling to show up correctly

<%= link_to "Sign In", new_user_session_path, :method => :get, style: "color:white" %> <h3>Read More - <%= link_to 'Article', action: :articles, style: "color: purple" %></h3> <%= link_to 'Click to Read More Abo ...

How can I design unique navigation menus using HTML, CSS, JavaScript, and jQuery?

Is there a way to create menus where clicking on a holiday/seasonal category opens up all related lists automatically? For example: https://i.sstatic.net/Nctd1.png I've been searching online for submenu options but haven't found the right metho ...

Adjust the size of a div while maintaining its aspect ratio based on the width and height of its parent element using CSS

After exploring various methods to create div aspect ratios using CSS, I had success with a demo. However, I encountered an issue with setting the height of my container when the width-to-height ratio is larger (#1 scenario). https://i.sstatic.net/VNdJ5.p ...

Creating Dynamic Web Design: Horizontal Scrolling Window and Adaptive HTML Content

After spending countless hours researching online, I am still unable to find a solution for responsive web design that maintains the aspect ratio of both the window and HTML body. Here are examples of what I'm looking for: original view, stretched le ...

What is the best way to upgrade npm on a Windows operating system?

I followed the steps from this website: sudo npm cache clean -f sudo npm install -g n sudo n stable Unfortunately, it was not successful for me. Can someone guide me on how to perform these commands on a Windows operating system? ...

Node.js is not supported by npm

npm is having compatibility issues with Node.js versions 15.5.0 and 14.15.3 Current npm Version: 5.6.0 I attempted to upgrade using the command: npm i -g npm-upgrade, but I am still stuck on version 5.6.0 I have experimented with various Node.js version ...

browsing through a timeline created using HTML and CSS

I am currently working on a web project that involves creating a timeline with rows of information, similar to a Gantt chart. Here are the key features I need: The ability for users to scroll horizontally through time. Vertical scrolling capability to na ...

What causes Angular to consistently redirect to the homepage?

Whenever I attempt to access the '/' route, it consistently displays the static-root-component (the main page component). However, if I try to access the '/welcome' route, it immediately redirects back to '/' and loads the sta ...

Utilize ng-bootstrap in an Angular CLI project by integrating it with npm commands

I've been attempting to set up a project using Angular CLI with ng-bootstrap, but I'm having trouble getting the style to work properly. Here are the exact steps I followed (as outlined in the get-started page): Create a new project using `ng n ...

Searching for the precise error name being thrown in MySQL using Node.js

Currently, I am in the process of constructing a server using node.js (express) and mysql felix. The issue I am facing is that occasionally I encounter a duplicate error when running a certain query. I have exhausted all of my attempts to handle this err ...

Adjust text alignment of SVG elements using CSS

I am facing an issue where I am unable to change the x and y variables of Svg text tags using CSS. This should work as it does with Svg and . However, I am only able to make changes if I directly add the x and y positions in the HTML code. Html: <svg ...

The access to Angular.js has been denied with a 403 Forbidden error

I'm seeking assistance with a specific issue I am facing. For an upcoming job interview, I have been tasked with cloning and understanding a project - Repository HUBTaxi-Server. I am struggling as this project is larger than what I am used to working ...

removing spaces in mongodb aggregation function

In the MongoDB database, there is an object with city names that contain spaces in between. Here's an example: { "_id": ObjectId("65ofb9104b1cf1519e4c5957"), "country": "us", "city": " ...

Is it possible to change the file name of vendors.js using webpack?

During the process of compiling a Vue.js project with Webpack, I need to make adjustments to the filenames. I am currently working with webpack version 4.x.x and vuejs version 2.x.x. Specifically, I have the requirement to change the names of "vendors.js ...

How to center align an input vertically with Bootstrap 3

I am having trouble vertically aligning an input in my project. Interestingly, my code works for a span but not for the input! I'm puzzled - what could be the reason behind this? JSFiddle Here is the CSS code snippet: .float { display: table-cel ...