Node.js fs.fsync() Method
Last Updated :
21 Aug, 2020
Improve
In Node, the 'fs' module provides an API for interacting with the file system in a manner closely modeled around standard Portable Operating System Interface (POSIX) functions.
It has both synchronous and asynchronous forms. The asynchronous form always takes a completion callback as its last argument. The arguments passed to the completion callback depend on the method, but the first argument is always reserved for an exception. If the operation was completed successfully, then the first argument will be null or undefined. The fs.fsync() method is a kind of asynchronous form. Synchronizes a file with the one stored on the computer.
javascript
Output:
javascript
Run index.js file using the following command:
Syntax:
fs.fsync(fd, callback);Parameters: This method accepts two parameters as mentioned above and described below:
- fd: It is a file descriptor (integer) to get in a synchronous way.
- callback: It is a callback function to check whether any error has occurred.
// Requiring module
const fs = require('fs');
// Opening a file
const fd = fs.openSync('example.txt', 'r+');
// Function call
fs.fsync(fd, (err) => {
if(err) {
console.log(err);
} else {
console.log("FD:",fd);
}
})
FD: 3Example 2: Filename: index.js
// Requiring modules
const fs = require('fs');
const express = require('express');
const app = express();
const fd = fs.openSync('example.txt', 'r+');
app.get('/', (req, res) =>{
});
// Function call
fs.fsync(fd, (err) => {
if(err) {
console.log(err)
} else {
console.log("FD:",fd)
}
});
// Server setup
app.listen(3000, function(error){
if (error) console.log("Error")
console.log("Server listening to port 3000")
})
node index.jsOutput:
Server listening to port 3000 FD: 3Reference: https://nodejs.org/api/fs.html#fs_fs_fsync_fd_callback