Create a backend in Javascript: NodeJS Files and Folders Manipulation

Here is a series of articles to help you create backend applications in Javascript.

Node.js is now a must, so it is essential for a developer to master it.

I will publish a new article every two days and little by little you will learn everything the…


This content originally appeared on DEV Community and was authored by Eric The Coder

Here is a series of articles to help you create backend applications in Javascript.

Node.js is now a must, so it is essential for a developer to master it.

I will publish a new article every two days and little by little you will learn everything there is to know about Node.js

To not miss anything follow me on twitter: https://twitter.com/EricTheCoder_

Modules included with NodeJS

NodeJS comes with several modules with functions that allow you to easily perform the most common tasks.

You will see later that it is also possible to add other modules created by the community and extend the functionality of your application almost unlimited.

Here I will present you only a few examples of NodeJS modules. You can consult the NodeJS documentation for full details of the modules and how they work. https://nodejs.org/en/docs/

The Path module

The 'path' module contains functions that allow proper handling of folder and file locations

To reference a module, just use its name

const path = require('path')

products_path = '/data/products/products.json'
products_file = path.basename(products_path)

console.log(products_file) // products.json

The path.basename() function allows you to return the name of the file only so in this case 'products.json'

Another handy function is path.join(). This function allows you to join together one or more folders and files. example :

const path = require('path')

data_folder = '/data/'
products_folder  = '/products'
products_file = 'products.json'

const full_path = path.join(data_folder, products_folder, products_file)

console.log(full_path) // /data/products/products.json

path.join() concatenates all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path

Finally, sometimes you would like to have the absolute path on the server

const path = require('path')

data_folder = '/data/'
products_folder  = '/products'
products_file = 'products.json'

const full_path = path.join(data_folder, products_folder, products_file)

const abs_path = path.resolve(__dirname, 'data', 'products', 'products.json')

console.log(abs_path)
// /Users/username/Documents/dev/learn_node/data/products/products.json

path.resolve() process the sequence of paths from right to left, with each subsequent path prepended until an absolute path is constructed.

The File System module

Undoubtedly one of the most used module, this module allows you to handle files and folders on the server

The FS module allows the manipulation of files and folders in two different ways. You can do this in synchronous or asynchronous mode.

Synchronous functions

This means that this function is blocking, NodeJS will wait for the function return value before resuming the execution of the application.

Asynchronous functions

This means that NodeJS will not wait for the function return value, it will continue your application execution and when the function is finished, will process the result.

Which method to use?

It depends on the type of application you want to develop. If for example you are developing a web server in this case it is preferable, not to say essential, to use an asynchronous function. A synchronous function would block the execution of the server not only for the current user but also would block all users.

On the other hand in certain very precise cases the use of a synchronous function can be justified. For example, before launching a web server, if you need to read a configuration file, in this case, a synchronous function guarantees that you will read the file before the server is launched.

In short, in general, always used an asynchronous function and only if necessary, a synchronous function.

Let's see an example of using the 'fs' module with the two types of functions:

Synchronous function

// app.js

const fs = require('fs')

const data = fs.readFileSync('info.txt', 'utf-8')
console.log(data) // file content
console.log('The file has been read')

Here the result is easily predictable, the code will be executed line by line.

Asynchronous function

const fs = require('fs')

const info = fs.readFile('info.txt', 'utf-8', (err, data) => {
    console.log(data)
})
console.log('The file has been read')

Here, NodeJS will not wait for the function to return to continue execution.

This will have the consequence of displaying 'The file has been read' first and when the readFile() has finished its work, NodeJS will execute the callback function console.log(data)

Reading and creating a file

Note that for the rest of this tutorial we will only use asynchronous functions.

First we will create a text file. To do this we will use the writeFile function

const fs = require('fs')

const data = 'This is my Hello World file'

fs.writeFile('info.txt', data, 'utf-8', (err) => {
    console.log('File created')
})

The writeFile () function is quite self-descriptive. You must specify the file name, data and encoding option. (utf-8 for text)

If you run this code, the info.txt file will be created in the current folder.

Then it will be possible to read this file with the readFile function

const fs = require('fs')

const info = fs.readFile('info.txt', 'utf-8', (err, data) => {
    console.log(data)
})

If the file does not exist you can return an error

const info = fs.readFile('info.txt', 'utf-8', (err, data) => {
    if (err) 
        console.log(err)
    else
        console.log(data)
})

It is also possible to read a file with readFile but using promises.

const fs = require('fs').promises

const start = async () => {
    const data = await fs.readFile('info.txt', 'utf8')
    console.log(data)
}

start()

Different import and different syntax but same result

Copy a file

To copy a file we use the copyFile function

fs.copyFile('info.txt', 'info2.txt', (err) => {
      if (err) return console.error(err)
        console.log('File copied')
})

Create a folder

To create a folder we use the mkdir function

fs.mkdir('data', (err) => {
    console.log('Data folder created')
})

The folder is created inside the current folder

List files in a folder

It is possible to get the list of files in a folder

fs.readdir('.', (err, files) => {
    console.log(files)
})

'.' represents the current file
files is a array containing all folder file name

Rename a file

To rename a file we use the rename() function

fs.rename('info.txt', 'data.txt', (err) => {
    if (err) return console.log(err)
    console.log('File renamed')
})

The possibilities are almost endless!

You now have a base with the 'fs' module if you want to know all the available 'fs' functions, consult the NodeJS website for all the details:
https://nodejs.org/api/fs.html

Conclusion

That's all for today, follow me on twitter: https://twitter.com/EricTheCoder_ to be notified of the publication of the next article (within two days).


This content originally appeared on DEV Community and was authored by Eric The Coder


Print Share Comment Cite Upload Translate Updates
APA

Eric The Coder | Sciencx (2021-10-04T09:46:56+00:00) Create a backend in Javascript: NodeJS Files and Folders Manipulation. Retrieved from https://www.scien.cx/2021/10/04/create-a-backend-in-javascript-nodejs-files-and-folders-manipulation/

MLA
" » Create a backend in Javascript: NodeJS Files and Folders Manipulation." Eric The Coder | Sciencx - Monday October 4, 2021, https://www.scien.cx/2021/10/04/create-a-backend-in-javascript-nodejs-files-and-folders-manipulation/
HARVARD
Eric The Coder | Sciencx Monday October 4, 2021 » Create a backend in Javascript: NodeJS Files and Folders Manipulation., viewed ,<https://www.scien.cx/2021/10/04/create-a-backend-in-javascript-nodejs-files-and-folders-manipulation/>
VANCOUVER
Eric The Coder | Sciencx - » Create a backend in Javascript: NodeJS Files and Folders Manipulation. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/04/create-a-backend-in-javascript-nodejs-files-and-folders-manipulation/
CHICAGO
" » Create a backend in Javascript: NodeJS Files and Folders Manipulation." Eric The Coder | Sciencx - Accessed . https://www.scien.cx/2021/10/04/create-a-backend-in-javascript-nodejs-files-and-folders-manipulation/
IEEE
" » Create a backend in Javascript: NodeJS Files and Folders Manipulation." Eric The Coder | Sciencx [Online]. Available: https://www.scien.cx/2021/10/04/create-a-backend-in-javascript-nodejs-files-and-folders-manipulation/. [Accessed: ]
rf:citation
» Create a backend in Javascript: NodeJS Files and Folders Manipulation | Eric The Coder | Sciencx | https://www.scien.cx/2021/10/04/create-a-backend-in-javascript-nodejs-files-and-folders-manipulation/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.