1

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'
Rahman Haroon
  • 1,088
  • 2
  • 12
  • 36
  • I think you need to double-check your description of the local filesystem. It does not appear to be formatted the way you intended. – Tom Sep 04 '20 at 18:07
  • i fixed that issue..now please check the directory of my local file system i mentioned above – Rahman Haroon Sep 04 '20 at 18:23
  • You still haven't fixed your FS code. It does not display the way you intended. I can tell because I'm able to edit your post and see the original text you wrote. If you want people to help you for free, you would do well to make sure that your post accurately reflects what you intended. It is obvious you did not compare what you typed in to how it displays. SO shows a live preview as you type. This doesn't give me hope that you'll be able to make much use of any help you receive, and that you'll need someone to do 100% of the work for you instead of just helping you get past one problem. – Tom Sep 04 '20 at 18:27
  • Yeah,I used fs.readFile as u tell, but its still showing error...Callback must be a function – Rahman Haroon Sep 04 '20 at 18:34
  • I've fixed the formatting of your post. Hopefully this will make it possible for someone else to help you. – Tom Sep 04 '20 at 18:36
  • thank you for fixing my post.. – Rahman Haroon Sep 04 '20 at 18:41

2 Answers2

-1

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 ).

Tom
  • 8,509
  • 7
  • 49
  • 78
  • I'm sorry but I don't understand what you mean. Please update your post, fix the filesystem drawing, and try to explain better what it is you're hoping to accomplish. – Tom Sep 04 '20 at 18:11
  • You should consult the documentation for Path.resolve, Path.join, and String.prototype.replace. Those are the tools you will probably want to use. Node docs: https://nodejs.org/api/path.html String docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace – Tom Sep 04 '20 at 18:38
-1

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'}))
Rahman Haroon
  • 1,088
  • 2
  • 12
  • 36