Dealing with a routing issue in node.js/express involving JavaScript and CSS

I'm facing an issue. I need to set up a route from localhost.../cars to localhost../bmw/x1

On the localhost../cars page, there's a button that, when clicked, should load localhost../bmw/x1

This is the JavaScript code I have:

const express = require("express");


var app = express();


app.use(express.static("public"));


app.set("views", "views");
app.set("view engine", "pug");

app.get('/', (req, res) => {
    res.render("firstpage");
});


app.get("/cars", function(req,res){
    res.render("cars");
});


app.get("/bmw/x1", function(req,res){
  res.render("bmwx1");
});

app.listen(PORT, function() {
  console.log("server is running);
});

In my pug file for the firstpage, I have the following structure (only part of the code):

html 
    head 
        link(rel="stylesheet", href="styles/style.css")
        link(href="https://fonts.googleapis.com/css2?family=Akshar:wght@300&display=swap" rel="stylesheet")
        meta(name="viewport", content="width=device-width, initial-scale=1, maximum-scale=1")
    body
        nav 
            h3 
                a(href="/cars") cars

And on the /cars page, there's a button like this:

div
   button(class="detailsbutton") 
          a(href="/bmw/x1") Details

Although everything seems to work fine, when I navigate to localhost../bmw/x1, the CSS doesn't apply properly only in that path, causing formatting issues.

I'm not certain if using app.get("/site/secondsite",...) is the correct approach or if the problem lies elsewhere.

Thank you for your help and apologies for any language errors!

Answer №1

When encountering a 404 error, it could be due to the absence of a / before your CSS link.

  • Adding a slash at the beginning of the styles indicates that it is being called from the root directory.
  • To resolve this issue, modify your link to href="‌/styles/style.css" and it should function correctly.

For further information, refer to this source.

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

A tag that does not adhere to the background color's rgba opacity restrictions

Can you believe what's happening to me? Check out this code snippet. I'm trying to apply an rgba color to my a tag, but it's acting like an rgb color instead of the intended rgba. The text background is solid, but the rest of the background ...

Ways to retrieve an array following a function call

After a lot of testing and troubleshooting, I finally got my array to function properly in the console. However, when I click the button, the array is displayed on the console but not in my HTML. TS: jogar(){ for(var u=0;u<6;u++){ this.y ...

Dispatching identification to controller after submission

I am encountering an issue with a page that contains the following code: <div class="col-sm-2 displaybutton"> <div data-point-to="displaysHeader"> @using (Html.BeginForm("Administration", "Home", FormMethod.Post)) ...

When new text is added to Div, the first line is not displayed

One of the divs on my page has an ID of "TrancriptBox" and contains multiple lines of text. When I scroll through it on my iPad, everything works fine. However, if I scroll and then change the text within that div, it doesn't display the first line o ...

Prevent any errors related to the remixing of `ErrorResponseImpl`

I am currently using remix on an express server with nodejs Whenever a route is not found, this message appears in the console: ErrorResponseImpl { status: 404, statusText: 'Not Found', internal: true, data: 'Error: No route matches ...

The ultimate guide to loading multiple YAML files simultaneously in JavaScript

A Ruby script was created to split a large YAML file named travel.yaml, which includes a list of country keys and information, into individual files for each country. data = YAML.load(File.read('./src/constants/travel.yaml')) data.fetch('co ...

Unable to execute a basic opacity transition on a div element

Why isn't the opacity transitioning in Chrome? I only want it to fade in with the 'transitions' class without fading out first. Check out this code sample: http://jsfiddle.net/chovy/t37k3/9/ <div></div> <a href="#" class="s ...

Sending numerous arguments to getStaticPaths() in nextjs

I am looking to create two different routes: /midterm/cs611 /finalterm/cs611 My goal is to display distinct content when accessing the /midterm/cs611 endpoint, and different content when accessing the /finalterm/cs611 endpoint. However, I am running into ...

Steer clear of using inline styling when designing with Mui V5

I firmly believe that separating styling from code enhances the clarity and cleanliness of the code. Personally, I have always viewed using inline styling (style={{}}) as a bad practice. In Mui V4, it was simple - I would create a styles file and import i ...

What is the best way to align these two divs centrally within a container set to display as inline-block?

I can't seem to get these two floated left divs centered within a container that has a height set to auto and a display of inline-block. The container has a max-width of 1600px, but when the window is stretched wider than that, it stays aligned to the ...

Troubleshooting: Issues with jQuery JavaScript Modal Popup functionality within MVC framework

When I click on the "Comments" link, a modal popup opens up and displays the content as expected. Now onto my issue: In the first scenario, the desired outcome is achieved but some other code does not execute. In this case, when I place the "@section" ins ...

What is the most efficient way to use the $slice operator on a highly nested array in mongoose

I am currently working on slicing a deeply nested array. To illustrate, consider the following structure. I aim to slice this array for pagination purposes. {messages: [{ message: { members: [ {example: object, blah: blah}, {example2: object2, blah2: blah ...

Kendo Grid with locked height

Using a grid with some elements locked, we have established a CSS-defined minimum and maximum height for the grid. .k-grid-content { max-height: 400px; min-height: 0px; } An issue arises when setting the height of the locked grid. If the grid&a ...

Is it possible to make a call to an endpoint from within the node project?

I'm currently working on a MERN app that dynamically adds meta tags to React pages without using server-side rendering. In order to achieve this, I need to extract the query parameters from the main server file and assign the relevant metadata content ...

struggling with the installation of bcrypt using npm

When attempting to install bcrypt using npm on my Windows system, I encountered some errors that are preventing the installation. I tried several different commands, but unfortunately none of them were successful: npm install --save bcryptjs && n ...

How to read files string-by-string and perform operations in NodeJS using fs.readFileSync?

Apologies for the simple question, How can I read from a file in NodeJS, string by string, extract values such as URLs, and perform operations on each string? var contents = fs.readFileSync('test.txt', 'utf8'); What should I do next? ...

How can I request a discord bot to perform a task from a different application?

Currently, my discord.js bot is up and running on a node server. I am looking to have it perform various actions (beyond just sending messages) in response to events occurring in another application that may not necessarily be on the same server. I am won ...

Issue encountered when using exports in a React project - Uncaught ReferenceError: exports is not recognized

Recently, as I began my journey with React.js, I encountered a perplexing error. Within my project, there is a file that exports a function in the following format: exports.building = { //... //Something goes here... }; To import this function, I uti ...

What is the method for retrieving the status of an xmlHttpRequest?

I'm curious about how to verify the status of an xmlHttp request once it has been sent. Can someone explain the process to me? Thank you. function sendRequest(){ //retrieve access token var accessToken = 'xxx'; //get user_id ...

Transform an array containing arrays into an array of individual objects

After spending a considerable amount of time trying various solutions, I am still unable to achieve the desired result. The issue lies in getting an array of objects from service calls and organizing them into a single array for data loading purposes. The ...