recently i tried to write an express js and node js server.
I have seen on the internet that in order to serve images, i have to set the static path using express.static. Only it does not seem to work, no matter what i try.
I have express installed both locally and globally.
I have tried to change the argument of express.static from “/public” to __dirname + "/public"
or path.join(__dirname, "public")
This is my index.js:
const express = require("express")
const app = express()
app.get(express.static(__dirname + "/public"));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html')
})
app.listen(3000, () => console.log('Listening!!'))
This is my index.html in /public/:
<html>
<body>
<img src="download.png" alt="">
</body>
</html>
This is a scheme of the folders hierarchy:
/node/:
---> /node_modules/
/public/:
---> index.html
download.png
index.js
package.json
And here is a screenshot of the result:
Result