3

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 }

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • My 2 cents here, could it be somehow related to this issue? https://stackoverflow.com/questions/44894236/express-sending-assets-with-wrong-mime-type, maybe you have to specify a mount path, like this `app.use('/static', express.static(path.resolve(__dirname, '../client/dist/static')));` – Alejandro Vales Sep 08 '18 at 09:44
  • 1
    @AlejandroVales thanks for your effort, but the first answer got it right. – Octavian Niculescu Sep 08 '18 at 09:50

1 Answers1

1

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);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • That actually worked but I don't really get why. Is app.use(express.static('public')); a route so it's getting overwritten if it's not the first or what? ////nevermind, I got it. I will accept your answer ASAP. Thanks a lot. – Octavian Niculescu Sep 08 '18 at 09:45