Create/Read/Write File using File Server: Node.js


Lets learn how to Create, read and write a file using file server module of Node.js

file-server-fs-create-read-write-file-nodejs

In this video tutorial we shall learn creating a folder/directory, creating a file, reading from a file, writing to a file and copying content from one file to another. We shall also see how you can look at all other facilities provided by fs module and make use of it in your real-time node.js web applications.

JavaScript: Reading a file
app.js

1
2
3
4
5
var fs= require("fs");
 
var html= fs.readFileSync("index.html", "UTF-8");
 
console.log(html);

Here we require fs module of node.js Using the readFileSync method of fs object, we open and read the file presented to it as its first argument. Second argument is the character-set type.

HTML FILE
index.html

1
2
3
<html>
<h1>Technotip.com</h1>
</html>

This is the html source code which will be fetched by app.js and being displayed on the console window upon execution.

JavaScript: Creating New File and Writing to file
app.js

1
2
3
4
var fs= require("fs");
 
var html= fs.readFileSync("index.html", "UTF-8");
fs.writeFileSync("satish.html", html);

Here we pass 2 arguments to writeFileSync method. First parameter being the file name – to be created, and the second parameter contains the content to be copied to the file we created.

JavaScript: Creating Directory/folder
app.js

1
2
3
4
5
var fs= require("fs");
 
fs.mkdir("satish");
 
console.log("Folder created!");

Pass name of the folder to be created to mkdir method of fs object.

JavaScript: Deleting a file
app.js

1
2
3
4
var fs= require("fs");
 
fs.unlink("satish.html");
console.log("File deleted");

Pass the existing file name to the unlink method and it’ll delete it. Make sure to give proper file name with correct file extension and path.

Create/Read/Write File using File Server: Node.js


[youtube https://www.youtube.com/watch?v=o7NIYGLC13o]

YouTube Link: https://www.youtube.com/watch?v=o7NIYGLC13o [Watch the Video In Full Screen.]



Note: There are plenty other file operation facilities provided by fs module, please use some other properties and methods from below list and let us know the code and what it does in the comment section below. This would surely help everyone in our reader community.

REPL of Node.js
Command Prompt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
C:\node>node
> fs = require("fs");
{ Stats: [Function],
  exists: [Function],
  existsSync: [Function],
  readFile: [Function],
  readFileSync: [Function],
  close: [Function],
  closeSync: [Function],
  open: [Function],
  openSync: [Function],
  read: [Function],
  readSync: [Function],
  write: [Function],
  writeSync: [Function],
  rename: [Function],
  renameSync: [Function],
  truncate: [Function],
  truncateSync: [Function],
  ftruncate: [Function],
  ftruncateSync: [Function],
  rmdir: [Function],
  rmdirSync: [Function],
  fdatasync: [Function],
  fdatasyncSync: [Function],
  fsync: [Function],
  fsyncSync: [Function],
  mkdir: [Function],
  mkdirSync: [Function],
  readdir: [Function],
  readdirSync: [Function],
  fstat: [Function],
  lstat: [Function],
  stat: [Function],
  fstatSync: [Function],
  lstatSync: [Function],
  statSync: [Function],
  readlink: [Function],
  readlinkSync: [Function],
  symlink: [Function],
  symlinkSync: [Function],
  link: [Function],
  linkSync: [Function],
  unlink: [Function],
  unlinkSync: [Function],
  fchmod: [Function],
  fchmodSync: [Function],
  chmod: [Function],
  chmodSync: [Function],
  fchown: [Function],
  fchownSync: [Function],
  chown: [Function],
  chownSync: [Function],
  _toUnixTimestamp: [Function: toUnixTimestamp],
  utimes: [Function],
  utimesSync: [Function],
  futimes: [Function],
  futimesSync: [Function],
  writeFile: [Function],
  writeFileSync: [Function],
  appendFile: [Function],
  appendFileSync: [Function],
  watch: [Function],
  watchFile: [Function],
  unwatchFile: [Function],
  realpathSync: [Function: realpathSync],
  realpath: [Function: realpath],
  createReadStream: [Function],
  ReadStream:
   { [Function: ReadStream]
     super_:
      { [Function: Readable]
        ReadableState: [Function: ReadableState],
        super_: [Object],
        _fromList: [Function: fromList] } },
  FileReadStream:
   { [Function: ReadStream]
     super_:
      { [Function: Readable]
        ReadableState: [Function: ReadableState],
        super_: [Object],
        _fromList: [Function: fromList] } },
  createWriteStream: [Function],
  WriteStream:
   { [Function: WriteStream]
     super_:
      { [Function: Writable]
        WritableState: [Function: WritableState],
        super_: [Object] } },
  FileWriteStream:
   { [Function: WriteStream]
     super_:
      { [Function: Writable]
        WritableState: [Function: WritableState],
        super_: [Object] } },
  SyncWriteStream:
   { [Function: SyncWriteStream]
     super_:
      { [Function: Stream]
        super_: [Object],
        Readable: [Object],
        Writable: [Object],
        Duplex: [Object],
        Transform: [Object],
        PassThrough: [Object],
        Stream: [Circular] } } }
>

Watch the above video to understand how to make use of above data in your own real-time node.js web application.

Leave a Reply

Your email address will not be published. Required fields are marked *