The sendFile()
method is not checking for file in public folder, it is still trying to find the file in C drive, even though I have set the static path.
How do I know that only sendFile()
is not checking for file in public folder? Because index.ejs file which is acting as homepage is using style.css from public folder, and it is working fine. When I search "localhost:5000/about.html", it is also working but "localhost:5000/about" is not working.
import express from "express";
import path from "path";
const app = express();
app.use(express.static(path.join(path.resolve(), "public")));
app.set("view engine", "ejs");
app.get("/", (req,res)=>{
res.render("index", {name: "Aniket"});
});
app.get("/about", (req, res)=>{
// res.sendFile(path.join(path.resolve(),"public","about.html"));
res.sendFile("about.html");
});
app.listen(5000, () => {
console.log("The server is working!");
});
The directory:(The above code is written in index.js)
>node_modules
>public
|-> about.html
|-> style.css
>views
|-> index.ejs
index.js
package-lock.json
package.json
This is the error that I am getting:
TypeError: path must be absolute or specify root to res.sendFile
at ServerResponse.sendFile (c:UserskumarOneDriveDesktopWeb DevelopmentPracticenode_modulesexpresslibresponse.js:441:11)
at file:///c:/Users/kumar/OneDrive/Desktop/Web%20Development/Practice/index.js:21:9
at Layer.handle [as handle_request] (c:UserskumarOneDriveDesktopWeb DevelopmentPracticenode_modulesexpresslibrouterlayer.js:95:5)
at next (c:UserskumarOneDriveDesktopWeb DevelopmentPracticenode_modulesexpresslibrouterroute.js:144:13)
at Route.dispatch (c:UserskumarOneDriveDesktopWeb DevelopmentPracticenode_modulesexpresslibrouterroute.js:114:3)
at Layer.handle [as handle_request] (c:UserskumarOneDriveDesktopWeb DevelopmentPracticenode_modulesexpresslibrouterlayer.js:95:5)
at c:UserskumarOneDriveDesktopWeb DevelopmentPracticenode_modulesexpresslibrouterindex.js:284:15
at Function.process_params (c:UserskumarOneDriveDesktopWeb DevelopmentPracticenode_modulesexpresslibrouterindex.js:346:12)
at next (c:UserskumarOneDriveDesktopWeb DevelopmentPracticenode_modulesexpresslibrouterindex.js:280:10)
at SendStream.error (c:UserskumarOneDriveDesktopWeb DevelopmentPracticenode_modulesserve-staticindex.js:121:7)
Edit: I understand that this might not be the best approach for using sendFile. I was used to working with PHP and MySQL and recently shifted to the MERN stack. While following a basic tutorial, I ran into this error.
(github repo – https://github.com/11aniketkumar/MERN_error)
2
Answers
After reviewing the response from @Mike'Pomax'Kamermans, I attempted to explore alternatives. As he suggested, the approach of using the
sendFile
method for redirecting to a new webpage is not recommended. I found that the code functioned correctly when I utilized theredirect
method instead.Despite attempting various solutions, I encountered challenges in making the
sendFile
method redirect to files located in the static directory without utilizing an absolute path. However, since my primary goal was to navigate the user to a different webpage, this method still works fine.This result also indicates that there's no issue with the
express.static
method, as the path is reachable using theredirect
method. The problem lies with thesendFile
method. I would have thought that the developers had restricted the use to only absolute paths with the root address, making it the exclusive method, or perhaps they had even eliminated support forexpress.static
. However, I've observed the same tutorial code functioning correctly with the exact same versions of Node.js, Express, and EJS. I'm beginning to suspect that this error is specific to my system.Your
express.static
setup is wrong.path.resolve
is a function, and needs some parameters to work. You havepath.resolve()
without any path. Try to change the lineapp.use(express.static(path.join(path.resolve(), "public")));
toapp.use(express.static(path.join(path.resolve("./public"))));