why static file handling in express js does not work for absolute path?

15 Views Asked by At

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>hello</h1>
    <img src="D:\New folder\f2\pexels-pixabay-35196.jpg" alt="logo" width="50">
</body>
</html>

server.js

const express = require ("express");
const fs = require("fs");
const app = express();
 app.listen(4444);

 //static file handling
 app.use(express.static(__dirname + "/folder"))
 app.use(express.static(__dirname + "/f2"))


 //get req -> app.get()


 app.get("/",(req, res)=>{
    fs.readFile("index.html","utf-8",(err,data)=>{
        if(err) console.log(err);
        else res.end(data);
     
   })
    //res.sendFile(__dirname + "/index.html")
 });
IN case IF I provide Absolute path for image , app.use(express.static(ImageFolder)) , image in img tag, the image does not displayed, But, if I provide relative path of image like this , it works fine as I handled the static file for the image

Question is that why does this realtive path is working fine, but absolute path is not wroking for static file handling in express js in node js?`

0

There are 0 best solutions below