1

So i have a problem. I want to make a route for example from localhost.../cars to localhost../bmw/x1

I have a button on localhost../cars, after clicking it, site loads localhost../bmw/x1

So have have this code in my javascript:

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);
});

and this in my pug where the firstpage is:(only a piece 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 then on /cars i have somewhere a button:

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

Now this actually works for me, it loads the pug but if i load into localhost../bmw/x1 my css doesn't work there, only on this path, anywhere it works but not there, so it's not formatted.

So im not sure if i can't just use app.get("/site/secondsite",...) and i have to do this other way or a error is somewhere else

Thanks for answering and sorry for my english!

1
  • 1
    The page /bmw/x1 tries to load /bmw/styles/style.css, because href="styles/style.css" is a relative URL. Make it absolute: href="/styles/style.css". Commented May 25, 2022 at 15:27

1 Answer 1

1

if you are receiving a 404 error is probably because you miss / in front of your CSS link

  • The slash in front of the styles means its calling from the root dir
  • change your link to href="/styles/style.css"it should work

for more detail

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.