I'm setting up a server on port XXXX which contains multiple route rendering ejs. In these ejs files I try to access pictures with relative path (basically "../public/images/images.png") but it says that i'm trying to access domain:port/path instead of domain/path.
Here's the architecture of my server folder:
backend/
- node_modules/
- views/
-- home.ejs
- public/
-- images/
---images.png
const app = express()
app.set('views', './views');
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '/public')));
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
});
app.get('/', function(req, res){
res.render('home');
});
[...]
var httpServer = http.createServer(app);
var httpsServer = https.createServer(options, app);
httpServer.listen(4000);
httpsServer.listen(8443);
File is missing because it's searched at domain:port/path (../public/images/images.png from views/home.ejs)
while I would like to search at domain/path
Try using absolute path instead relative path
change
../public/images/images.png
to
/images/images.png // because you have added line
app.use(express.static(path.join(__dirname, '/public')));
which directly refer to public folder for any of the static files.`