在Node中,‘fs’模塊提供了一種API,用於以圍繞標準便攜式操作係統接口(POSIX)函數緊密建模的方式與文件係統進行交互。
它具有同步和異步形式。異步形式始終將完成回調作為其最後一個參數。傳遞給完成回調的參數取決於方法,但是第一個參數始終為異常保留。如果操作成功完成,則第一個參數將為null或未定義。 fs.fsync()方法是一種異步形式。將文件與計算機上存儲的文件同步。
用法:
fs.fsync(fd, callback);
參數:該方法接受上述和以下所述的兩個參數:
- fd:它是一種以同步方式獲取的文件描述符(整數)。
- callback:它是一個回調函數,用於檢查是否發生任何錯誤。
返回值:此函數不返回任何值。
範例1: 文件名:index.js
// 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:3
範例2: 文件名: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")
})
使用以下命令運行index.js文件:
node index.js
輸出:
Server listening to port 3000 FD:3
參考: https://nodejs.org/api/fs.html#fs_fs_fsync_fd_callback
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js GM implode()用法及代碼示例
- Node.js GM drawPolygon()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
- Node.js GM edge()用法及代碼示例
- Node.js GM write()用法及代碼示例
- Node.js GM channel()用法及代碼示例
- Node.js GM roll()用法及代碼示例
- Node.js GM whiteThreshold()用法及代碼示例
- Node.js GM whitePoint()用法及代碼示例
- Node.js GM drawEllipse()用法及代碼示例
- Node.js GM threshold()用法及代碼示例
- Node.js GM chop()用法及代碼示例
- Node.js GM thumbnail()用法及代碼示例
- Node.js GM paint()用法及代碼示例
注:本文由純淨天空篩選整理自bunnyram19大神的英文原創作品 Node.js fs.fsync() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。