I'm trying create root dir and reach that by using
require.main.path
Return : C:\Users\HP\Desktop\Nodejs\sec-5
but when i try join this dir by using path.join
its ignoring the last parameter mean sec-5 folder.
path.join(rootDir, "views", "shop.html")
Return : C:\Users\HP\Desktop\Nodejs\views\shop.html
// path.js
const path = require("path");
console.log("path.js : 2", require.main.path);
module.exports = path.dirname(require.main.path);
// shop.js
const express = require("express");
const path = require("path");
const rootDir = require("../utils/path");
const router = express.Router();
router.get("/", (req, res, next) => {
console.log("shop.js : 7", path.join(rootDir, "views", "shop.html"));
res.sendFile(path.join(rootDir, "views", "shop.html"));
});
module.exports = router;
It seems like all your code is inside C:\user\HP\Desktop\Nodejs\sec-5
which means the path to your HTML file should be C:\user\HP\Desktop\Nodejs\sec-5\views\shop.html
, that's why it can't find your HTML file. A simple solution should be in path.js file, export the dirname
const path = require("path");
console.log("path.js : 2", require.main.path);
module.exports = require.main.path;