This is server.js which I am starting in cmd using node server.js:
const express = require("express");
const path = require("path");
const app = express();
const port = 3000;
app.use(express.static('/public'));
app.get("/", (req, res)=>{
res.sendFile(path.join(__dirname, "views/index.html"));
});
app.listen(port);
This is a section of index.html where I join it with css and create one image:
<!DOCTYPE html>
<html>
<head>
<title>Dućan s hranom</title>
<link rel="stylesheet" href="/public/styles/stil.css">
</head>
<body>
<div id="div1">
<img src="/public/images/logo.jpg" id="logo">
<span id="kat">Voće</span>
<div id="div2">
This is part of my error msg:
Refused to apply style from ‘http://localhost:3000/public/styles/stil.css’ because its MIME type (‘text/html’) is not a supported stylesheet MIME type, and strict MIME checking is enabled.
localhost/:10
GET http://localhost:3000/public/images/logo.jpg 404 (Not Found)
localhost/:18
GET http://localhost:3000/data/data.js net::ERR_ABORTED 404 (Not Found)
localhost/:1 Refused to execute script from ‘http://localhost:3000/data/data.js’ because its MIME type (‘text/html’) is not executable, and strict MIME type checking is enabled.
localhost/:91
This is what my directory looks like, but instead of ejs i use html for now
Started server with node server.js, and I was expecting it to open my html file and load css and images.
4
Answers
I believe you do not have to include
public
in the URL Express static files so by doinghttp://localhost:3000/public/images/logo.jpg
Express thinks you want/public/public/images/logo.jpg
, instead it should behttp://localhost:3000/images/logo.jpg
.You can also tell Express to look in multiple directories by doing
which means Express will also pickup your data folder. The choice is up to you whether you want to place your data folder inside of the public folder or just have multiple
app.use(express.static('directory_name'));
lines.The problem is:
/public path is not required and should be removed from the href tag. If you define
app.use(express.static('/public'));
then express will look for static files and directories from /public directory (for example/public/styles/
or/public/img/
etc).So the solution is to change your href tag value into:
The following two points are to take care:
Please use relative paths in frontend apps if the resources referred to are available in the same site of the html documents.
E.g. styles/stil.css
Absolute paths and Absolute URI(s) are used when it is the only way out.
The root passed into express.static can either be a relative path or an absolute path.
E.g. Relative path : express.static(“public”)
E.g. Absolute path : express.static(__dirname + “/”+ “public”)
For a detailed discussion on the same, please see the follow-up posted already.
Code revised
server.js
server.js
index.html
Citations:
What is the definition of an absolute URL (fully qualified?)
express.static(root, [options])
Please see below for a detailed explanation. This is in continuation to the short answer already posted to this question. Please start from there if not already done.
Quote from Express documentation
"The root argument specifies the root directory from which to serve static assets. The function determines the file to serve by combining req.url with the provided root directory."
Please note in the quote “…The function determines the file to serve by combining req.url with the provided root directory”.
Therefore the static file to serve will always be resulted as root + req.url
We have three cases to discuss.
case 1: when req.url is /public/styles/stil.css
case 2: when req.url is /public/images/logo.jpg
case 3: when req.url is /data/data.js
In all these cases, the root remains same as /public.
In case 1, the resulting static file to serve will be /public/public/styles/stil.css. It is as by the same formula root + req.url. This absolute path resulted will fail to load this resource since there is no such file in the server. It fails so because the actual file is in a relative path which is public/styles/stil.css. Therefore the absolute path is to be corrected. We shall address it in two steps.
Step 1 : req.url is corrected to styles/stil.css
Now let us look at the status:
static file = /public/styles/still.css
This also results an absolute path which will also fail to load since there is no file in the server in the resultant path.
Step 2: root is corrected to public
Now, static file = public/styles/still.css
This relative path will successfully load the resource since the file is existing in the
resultant path. This same discussion is applicable to the other two cases
as well.
Citations:
express.static(root, [options])