Node: cd myjourney02
Tags: javascript, node, devjournal, writing
Published: 2022-05-17 20:06:23

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. 
```javascript
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.

![Facts&Index](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/voxlj34i21d7ki1db15f.png)

And my server code 

![Server-Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8g4rlensk414d0d8n8vw.png)

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. 
```javascript 
  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. 
```javascript 
  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();
  });
}) 
```
So I run my server 
![Server-up](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m18hojkdayuxkounl8si.png) 

I request the default server page on port 8080
![Default](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/166mljq2zfpnus1vk80a.png)

Request a different path
![Different path](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gahv2ct6c15o5q46rqvq.png)

Request the **index.html** file in same folder as my server program.
![index.html](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txvz087q0cybhiityo4b.png)

I request the **facts.html** file in same folder too. 
![facts.html](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6ftz0c7l5jythfli29h.png)

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.