I learned about the file system module in Nodejs which allows Nodejs work with the file system on my computer (in my case, mobile storage) and makes Nodejs act as a file server.
What this basically means is that files (documents, videos, audios, etc) are stored on my computer (mobile) and this file system module allows Node access these files and serve them to client upon request (usually by the click of button).
In computing, file system or filesystem (often abbreviated to fs) is a method and data structure that the operating system uses to control how data is stored and retrieved. Without a file system, data placed in a storage medium would be one large body of data with no way to tell where one piece of data stopped and the next began, or where any piece of data was located when it was time to retrieve it
To enable this feature, just like the http and url modules, you include the file system module fs using the require() function and storing in variable.
var fs = require('fs');
So, I have two html files together with my server program in a folder named node as you can see below; facts and index.
And my server code
Looking at line 7, I noticed a dot wrapped in double quotes so I decided to find out why it's there and what it does, so I initiated my server and it crashed, a quick copy/paste of line 7 on Google brought up some interesting results.
The best I could take is that
The reason for the leading dot is that the logic of that example is to open a local file.
q.pathname will return something like /..., so adding a . in front of it, you'll get something like ./..., which identifies a file in the same directory where the node.js program is run.
Means that Nodejs as a file server needs the dot to recognise the path of file (e.g /index.html) as an entity (./index.html) within the same folder as my server program. It's best not to think of it in terms of relative or absolute path.
var filename = "." + q.pathname;
Line 8 has a lot of things happening, notice the fs module with a method readFile() taking two arguments, well the fs or File System module allows in:
Creating files using methods appendFile(), open(), writeFile()
Reading files using method readFile()
Updating files using methods appendFile(), writeFile()
Deleting files using method unlink()
Renaming files using method rename()
CR²UD
What the code means is that, " read whatever filename requested by client on my computer (as server) and respond with it.
A function passed as an argument which contains the error parameter and data parameter for if the file was not requested properly or if it doesn't exist and if the file was requested properly or it exists respectively. 400 is a status code to describe an error.
fs.readFile(filename, function(err, data) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
return res.end("404 Not Found");
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
})
I request the default server page on port 8080
Request the index.html file in same folder as my server program.
I request the facts.html file in same folder too.
Things learned
Combining the http,url and file system module to create a file server to serve files to client.
Modules
Resources: Google, W3Schools.com, Stackoverflow, Geeksforgeeks.org.
Top comments (0)