What is the reason behind Express.js not rendering CSS when the path includes two forward slashes?

When attempting to send a static page with just one '/' in the path, everything works smoothly. For instance:

app.get('/', (req, res) => {
    res.render('home.ejs')
})

The necessary CSS linked to the HTML page via the <link> attribute is rendered perfectly:

<link rel="stylesheet" href="style.css">

All HTML and CSS files are located in the same directory while the server.js file is in the preceding directory: https://i.sstatic.net/Kupxr.png

The server file is connected to the views directory through:

app.use(express.static(__dirname + '/views'));
app.set('views', path.join(__dirname, 'views'));

However, upon adding an additional '/' in the path, the CSS fails to render:

app.get('/projects/mcpaint', (req, res) => {
    res.render('mcpaint.ejs')
})

I tried embedding the CSS in the HTML using the <style> tag which surprisingly resulted in successful rendering of the CSS.

If anyone has suggestions on how to rectify this issue, I would greatly appreciate it! Thanks for any assistance!

Answer №1

If you haven't shared your template, all I can do is rely on my intuition, which indicates that you may be implementing something like

<link rel="stylesheet" href="public/style.css">

This would be relative to the URL of the current view; for example, it would fetch /projects/public/style.css if you are on /projects/projectx. A better approach would be to use

<link rel="stylesheet" href="/public/style.css">

to ensure it is rooted to the server's base.

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

Detecting Android devices

Issue: My website functions properly on desktop but has a problem with the carousel images skewing on iPhones. To address this, I created a separate CSS styling for iPhone devices and used a JavaScript function found online to detect iPhones and iPads. Un ...

picture located in the top corner of the page within the diva triangle shape

Is there a way to position my photo on the same level as a div and have it overlap slightly? <div id='triangle-topleft'> <img id="photo" src="photo.png"> </div> #triangle-topleft { width: 0; heigh ...

Looking to display the view source of an HTML page, but it keeps redirecting to another site when I try. Anyone know how to diagnose and fix this issue?

Can anyone assist me in displaying the HTML source of a page without it redirecting to another site? Here is the URL: ...

What is the best approach for adding text to an image using TCPDF?

I am trying to create dynvouchers PDF using TCPDF. However, I am facing an issue where the PDF generated does not include the username and password. Here is my code: $html = ''; $html .= '<table><tr>'; for ($a = 1; $a <= ...

Error encountered during npm installation: Unable to connect, ECONNREFUSED at address 127.0.0.1:808

Just embarking on my first Node.js app development journey, I encountered an error while trying to install Express through npm: Error: connect ECONNREFUSED 127.0.0.1:8080 at Object.exports._errnoException (util.js:870:11) at exports._exceptionW ...

Can templates be nested within other templates using different contexts?

I am in the process of developing an application using nodejs and handlebars. My goal is to create a layout template and insert various individual components into that layout. Each component will have its own handlebars template and context. e.g. layout. ...

What is the best way to obscure or conceal images of runners?

I am trying to figure out how to blur or hide the runner thumbnails on different streaming sites, such as Prime Video. I typically use the Amino: Live CSS Editor chrome extension or uBlock Origin to manipulate elements on websites. However, I am struggling ...

Passport is unable to include parameters for local authentication

I followed a tutorial on passport local authentication, which you can check out here: https://scotch.io/tutorials/easy-node-authentication-setup-and-local Everything is working as expected, but I want to enhance my user profiles by adding first and last n ...

Displaying numerous images/items in a Bootstrap 2.3 carousel

I have been working on a PHP-based website that utilizes Twitter Bootstrap 2.0. Currently, I am retrieving user information from a MySQL database and attempting to display them in separate frames or items within a carousel instead of all at once using a wh ...

Animate the jQuery display property to show a table without changing any specified spatial dimensions

When utilizing jQuery's $.animate() on an element styled with display:table, any spatial dimensions that are not explicitly specified to change will animate. Check out the fiddle here In the scenario presented, the width is defined for animation, bu ...

Exploring IP and port connections using Telnet in Java or JSP

I am in the process of creating a script that uses telnet to check if a server is currently up or down by connecting to its IP address and port number. I need this code to be highly efficient as it will be used within a for loop to display information on a ...

CSS an act of misbehavior

I have noticed some unusual behavior on my website page. My website design can be viewed at Also, here is the same design in CMS format Please pay attention to the section under <div class="godelpage"> <a href="http://ryugaku.babonmultimedia ...

What coding techniques can be used to create dynamic animation of a log stream in javascript/css

When I have a series of events in my browser, I want to display them as a list with a smooth animation effect. As each new event comes in, I'd like the existing items to slide down gracefully, making room for the new event to fade in at the top of the ...

magnificPopup experiencing difficulties when attempting to invoke a class that is dynamically generated within the HTML document

Currently, I am encountering an issue with the magnificPopup. Whenever I try to trigger the popup using the class 'popup-with-zoom-anim', it doesn't seem to work as expected. Has anyone else faced a similar problem before? <body> < ...

The Heroku application encounters issues with socket.io functionality, causing crashes

Everything was going smoothly with my application, an API server designed to interact with a Unity client, until I incorporated sockets. To test socket.io, I used the Chrome extension socket.io tester and it worked perfectly on my local machine. However, o ...

Error: The property 'remove' cannot be read from an undefined value

While working on a basic node project, I encountered an error that has me stumped. I can't seem to figure out how to resolve it or fully grasp its meaning. const expect = require('expect'); const request = require('supertest'); c ...

The session variable in Express.js remains static and does not update

` //logout router.get("/logout", (req, res) => { res.render("logout") req.session.username = undefined req.session.loggedIn = false }) //login router.get("/login", (req, res) => { res.render("login") }) router.post("/login", (req ...

Is it possible to have a <small> element nested within an HTML5 heading such as h1, h2, h3, etc?

I am curious about the proper way to structure headings in HTML5. Can I include a <small> element within an <h3> tag? For example: <h3>Payment details <small>(this is your default card)</small></h3> ...

The absence of C++ entry points is causing a lack of visibility in the profiling output of node

After utilizing the node --prof <command> command and then running node --prof-process on macOS, I noticed that my profiling output no longer displays any of the C++ entry points. This has resulted in significant gaps in my profiling data where certa ...

A picture placed side by side with a list

Could someone please help me figure out what I'm doing incorrectly? <div class="row"> <ul class='list-group .col-md-6'> <li>1</li><li>1</li><li>1</li> </ul> <div> ...