Skip to main content

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 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 required for the above codes to work.
var path = require('path');.


Comments

Popular posts from this blog

Talky Messenger Documentation & Setup

( Github ) Just created a chat app that runs in Node and Socket. the attempt was worthy. Talky is a messenger app built with Node, Express, Socket, Angular & Bootstrap. It's like a server-client structure. (But obviously not like the one we done at OS lab using shared memory). It has a broadcasting structure. Talky does not keep a log on chat. i.e., It doesn't have a memory or database. When we close the browser window, chat history is lost. There I also added a basic console, protected by a password, to send real-time notifications to active clients. The name 'Talky' was suggested by a friend of mine. (hey, thank you for that. The fact is that I am not really good at naming...😝) What if sometimes your college blocks WhatsApp? Try Talky. ( There is also a website on internet in the name of 'Talky' which has no connection with this one. ) Download Talky Messenger To use Talky, all you need is 3 things: Node server Source code

Repairing my keyboard gone wrong and then right.

Few months ago I tried to repair an old unused keyboard. I opened it cleaned and put everything back in place. But didn't work. Thinking it has met it's end-of-life, I gave it to my sister's daughter, 2 year old Komal. She went hard with it. Once she even thrown it. To my surprise, after a few throws, it's now working properly 🙆.

Formia - A Simulation of Google Forms. Part 1 - My Experience

This time I would like to share one of the exciting moments in my life, in which I understood what the Client - Server Concept means and by the Grace of God , simulated one of the Internet Giant's product, Google Forms from scratch. I finished the project a couple of months ago. Here is the story. (and the project ). Analysis page, See more screenshots Screenshots Install Formia. What it is? Formia is an incredible web application for Massive Feedback Analysis. Developed on May 2015. More than  4000 lines of codes. Flexible. Well structured. This can be viewed as a simulation of  Google Forms , a product hosted by the  Internet Giant, Google. Service available in  mobile, laptop, tabs . Languages Formia is constructed on  7 languages , including PHP, AJAX, JS, SQL etc. The Analysis part utilizes  Google APIs  which is one of the coolest (and  hottest ) feature. Usability Formia is optimized for easiness. User friendly just like Google For