0

I'm doing a simple node project to practice but i can't manage to serve html file with its css styles. I asked the same question before on SO but I didn't mention that the status of css file was "canceled" in the developer tools. When I edited it nobody answered about why it is canceled, so i wish someone help me with this. It is making me crazy. Here is my app.js code: app.use("/signup", express.static("public"));

app.get("/", (req, res)=>{
 res.send("Welcome to our website");
});

app.get("/signup", (req, res)=>{
 res.sendFile(`${__dirname}/index.html`)
})

Here is my html code:

<head>
    <title>testapp</title>
    <link href="./public/styles.css" type="text/css" rel="stylesheet"/>
</head>
<body>
    <form method="post" action="/signup">
        <input type="text" placeholder="*First Name" required></input>
        <input type="text" placeholder="*Last Name" required></input>
        <input type="number" placeholder="*Age" required></input>
        <input type="email" placeholder="*Email" required></input>
        <input type="submit" value="Submit" required></input>
    </form>
</body>

When I remove the get method of "/signup", and put my index.html in the public directory while updating the link to css, it works just fine, but i don't want it in the '/' route. And also when opening the index.html file without the localhost, it works fine. I searched about it and found someone who had kind of the same issue asked about on SO, I think that the answers aren't right in my case, i'm not sure and i didn't understand well.What does status=canceled for a resource mean in Chrome Developer Tools?.

Thank you in advance.

Eye Patch
  • 881
  • 4
  • 11
  • 23
  • Your html is invalid. The `` tag does not have a closing tag. In addition, the `` tag does not use and does not need a closing slash in HTML. – Rob Jul 25 '19 at 13:33

1 Answers1

2

Read the documentation about static serving here : https://expressjs.com/en/starter/static-files.html

You don't need to specify /public in your css link; just do as following :

<link href="/styles.css" type="text/css" rel="stylesheet"/>
Nathan Schwarz
  • 631
  • 5
  • 19
  • Note that the `` tag does not use and does not need a closing slash in HTML and never has. – Rob Jul 25 '19 at 13:35