Recently, it seems that Express is having trouble pulling images from the image folder in the public directory, even though it can successfully retrieve the styles.css file located in the css folder within the same public directory.
The public directory consists of two distinct folders - one designated for css files and another specifically for images.
I attempted to address this issue by including the line of code:
app.use(express.static('images'));
to directly target the image folder within the public directory. Unfortunately, this approach did not resolve the problem. While linking to images with a URL works fine, accessing images from the directory itself does not seem to be functioning as expected.
I would greatly appreciate any assistance or guidance on this matter. Despite conducting extensive searches online, I keep receiving the same recommendation to use express.static. Additionally, I experimented with adding __dirname
before 'public' but encountered errors in the process.
Below is an excerpt from my server code:
const express = require("express");
const bodyParser=require("body-parser")
const ejs = require('ejs');
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static('public'));
app.get("/", function(req, res){
res.render("index");
});
app.get("/prices", function(req, res, ){
res.render("prices");
});
app.get("/about", function(req, res, ){
res.render("about");
});
app.get("/contact", function(req, res, ){
res.render("contact");
});
const PORT = process.env || 3000;
app.listen(3000,function(){
console.log("server started")
});
Additionally, here is a snippet of my HTML template (header.ejs). The logo should be displayed here, however, it is currently not visible.
In an attempt to utilize the __dirname
, I implemented it in the following manner:
app.use(express.static(__dirname, 'public'));