Hey whassup! This time I'd like to resolve a mess I faced during coding.
There is a function called
We have to either use absolute path or specify
( Here is a good article about specifying paths in JavaScript : natashabanegas.com. But unfortunately this is not the solution in our case! )
In Express.js most of our works are concentrated on the
There are two simple ways to do it:
Importantly
There is a function called
sendFile()
in node. In use cases, it becomes res.sendFile(path_to_file)
. Specifying file path for res.sendFile()
from other directories is a bit tricky!We have to either use absolute path or specify
root
for the file location.( Here is a good article about specifying paths in JavaScript : natashabanegas.com. But unfortunately this is not the solution in our case! )
In Express.js most of our works are concentrated on the
routes
directory, which enables modular coding. So specifying paths for res.sendFile()
from routes is not as traditional as in the above linked article, but still simple.There are two simple ways to do it:
res.sendFile(path.join(__dirname, '../our_directory', 'our_file.html'));
res.sendFile('our_file.html', { root: path.join(__dirname, '../our_dir') });
__dirname
returns the directory that the currently executing script is in. So use the ../
or ./
stuffs according as your directory hierarchy.Importantly
path
is a built-in module, that needs to be require
d for the above codes to work.var path = require('path');
.
Comments
Post a Comment