Search code examples
javascriptnode.jsreactjsexpresssendfile

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


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'

Solution

  • 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'}))