简体   繁体   中英

My first app doesn't serve css even if I set express.static() and have a correct path

As the title says I'm trying to build my first node app and I'm encountering a problem.

Yesterday everthing worked fine. Now when I opened up my PC I get "Resource interpreted as Stylesheet but transferred with MIME type text/html: " http://localhost:3000/css.css "."

I find it very strange because I didn't edit anything, and it stopped working.

app.js

/*Libraries*/

var express = require("express"),
    app     = express()

/*Routes*/
    var indexRoute = require("./routes/index");
    app.use(indexRoute);
/*Modeles*/


/*Configuration*/
app.set("view engine", "ejs");
app.use(express.static('public'));

app.listen(3000, function() {
    console.log("==================================");
    console.log("===Serverul a pornit cu succes!===")
    console.log("==================================");
});

index.js

var express = require("express"),
    router = express.Router()

router.get("*", function(req, res) {
        res.render("index/404");
});

module.exports = router;

404.ejs

<% include ../../partials/header %>
    <section id="container-404" class="container-fluid">
        <div class="row">
            <div class="col-12">
                <h1 class="display-1">404</h1>
            </div>
        </div>
    </section>
<% include ../../partials/footer %>

header.ejs

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="/reset.css">
    <link rel="stylesheet" href="/fontawesome-all.min.css">
    <link href="https://fonts.googleapis.com/css?family=Alegreya+Sans:400,800" rel="stylesheet">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <link rel="stylesheet" href="/css.css">
    <title>Best DavNic73</title>
</head>
<body>

Folder structure:

root { app.js

partials > footer.ejs, header.ejs

public > css.css and the other 2 css files

routes > index.js

views > index > 404.ejs }

You currently got the following routing -> 404 -> public . That means that the 404 route catches all the requests, the public route cannot be reached. You have to mount the handlers in the right order:

  app.use(express.static('public'));
  app.use(indexRoute);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM