Node.js File System Module::
--------------------------
The Node.js file system module allows you to work with the file system on your computer.
To include the File System module, use the require() method:
var fs = require('fs');
Common use for the File System module:
Read files
Create files
Update files
Delete files
Rename files
Create Files ::
The File System module has methods for creating new files:
fs.appendFile()----> method appends specified content to a file.
If the file does not exist, the file will be created.
fs.open()---> method takes a "flag" as the second argument, if the flag is "w" for
"writing", the specified file is opened for writing. If the file does not exist, an
empty file is created
fs.writeFile() --->method replaces the specified file and content if it exists.
If the file does not exist, a new file, containing the specified content,
will be created.
fs.readFile() ----> to read the content of the file.
Delete Files ::
To delete a file with the File System module, use the fs.unlink() method.
Rename Files ::
To rename a file with the File System module, use the fs.rename() method.
Node.js URL Module ::
------------------
The URL module splits up a web address into readable parts.
To include the URL module, use the require() method:
var url = require('url');
Parse an address with the url.parse() method, and it will return a URL object
with each part of the address as properties.
File uploading in Node.js:
-------------------------
The Formidable Module
There is a very good module for working with file uploads, called "Formidable".
The Formidable module can be downloaded and installed using NPM:
C:\Users\Your Name>npm install formidable
After you have downloaded the Formidable module, you can include the module in
any application:
var formidable = require('formidable');
Steps to perform ::
Step 1: Create an Upload Form
Create a Node.js file that writes an HTML form, with an upload field.
Step 2: Parse the Uploaded File
Include the Formidable module to be able to parse the uploaded file once it
reaches the server.
When the file is uploaded and parsed, it gets placed on a temporary folder
on your computer.
Step 3: Save the File
When a file is successfully uploaded to the server, it is placed on a temporary folder.
The path to this directory can be found in the "files" object, passed as the third
argument in the parse() method's callback function.
To move the file to the folder of your choice, use the File System module,
and rename the file:
Comments
Post a Comment