I am trying to access a folder outside of the current directory of my folder in express js

Viewed 902

Their is two folders inside my folder one is made up for front-end and one is for back-end

project
├── back-end
│   ├── public
│   └── routes
│       ├── Calling.js
│       └── index.js
└── front-end
    ├── public
    └── src
        └── Components
            └── Contact.js

from back-end am trying to call front end file by using sendFile()

app.get('/', function(req,res,next){
    
    res.sendFile(path.join(
        __dirname,
        '../back-end',
        '/front-end/src/Components/Contact'
    ))
   
})

while am running (npm start) the folder is not switch back to front-end,It is considering as a folder of back-end and showing no such file directory

Here is the error message

ENOENT: no such file or directory, stat 'D:\Project\back-end\routes\front-end\contact-form\src\Components\Contact'
2 Answers

I fixed this isuue by using

res.sendFile(path.format({
        dir: 'D:\\Project\\front-end\\src\\Components',
        base: 'Contact.js'
      }))

Here I used

(path.format({dir:'path_of_file',base:'name_of_file'}))

Path.join does not combine the contents of two files, it combines two filesystem paths into a single path.

If you want to combine the contents of both files, you must read the contents of each file separately and then concatenate them together.

You want FS.readFile( path, options ).

Related