Skip to main content

Posts

Showing posts from February, 2016

Specifying Paths in Express.js

Hey whassup! This time I'd like to resolve a mess I faced during coding. 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') }); The __dirname returns the directory that the current