File System Module: Read Files (readFile()), Create Files (appendFile(), open(), writeFile()), Update Files (appendFile(), writeFile()), Delete Files (unlink()), Rename Files (rename())

fs module server ને memory આપે છે: err-first callback સાથે readFile, appendFile add (અને create) કરે છે, writeFile replace કરે છે, unlink delete કરે છે, rename renames કરે છે: registrations finally restart ને survive કરે છે.

12 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

server જેને amnesia છે

FestConnect નો server registrations ને beautifully લે છે: અને first restart પર તેમાંથી દરેક ને forget કરે છે. તેઓ variable માં રહેતા હતા, અને variables process સાથે die થાય છે.

Mehta-shop economics અહીં પણ apply થાય છે: data જે matters કરે છે તે disk પર જાય છે. Node નું built-in fs (File System) module એ pen છે: તમારો server દરેક registration ને file માં write કરશે, list ને back read કરશે, અને day ના end પર તેને archive કરશે. પાંચ operations, એક module, અને subject નો last lesson.

At a glance

fs verbs (add-vs-replace row ને twice learn કરો)

JobMethodBehaviour
Readfs.readFile(path, 'utf8', cb)તમારા callback ને content ને Deliver કરે છે
CreateappendFile / open(path, 'w') / writeFileબધા 3 file ને create કરે છે જો તે missing હોય
Update: addfs.appendFile(path, text, cb)end પર text ને ADDS કરે છે
Update: replacefs.writeFile(path, text, cb)આખું content ને REPLACES કરે છે
Deletefs.unlink(path, cb)file ને Removes કરે છે
Rename / movefs.rename(oldPath, newPath, cb)Renames કરે છે; path માં new folder તેને move કરે છે

Practical

registration book, full cycle

const fs = require('fs');

// CREATE + UPDATE(add): appendFile file ને create કરે છે જો missing હોય
fs.appendFile('registrations.txt', 'Riya: Garba Night\n', function (err) {
    if (err) throw err;              // err-first: trusting પહેલાં check કરો
    console.log('Registration saved');
});

// READ: asynchronous, content callback માં arrive થાય છે
fs.readFile('registrations.txt', 'utf8', function (err, data) {
    if (err) throw err;
    console.log('So far:\n' + data);
});

// UPDATE(replace): writeFile wipe અને rewrite કરે છે
// fs.writeFile('winner.txt', 'Team Pixel', function (err) { ... });

// DELETE અને RENAME:
// fs.unlink('old-draft.txt', function (err) { ... });
// fs.rename('registrations.txt', 'day1-archive.txt', function (err) { ... });

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Theory

err-first callback: Node નો handshake

દરેક fs callback err ને તેના first argument તરીકે receive કરે છે: null જ્યારે all well ગયું હોય, Error object જ્યારે નહીં (file missing, disk full, no permission).

function (err, data) { if (err) throw err; ... }

આ err-first convention એ Node-wide law છે: data ને touch કરતા પહેલા err ને check કરો, દરેક time: unchecked err એટલે data undefined છે અને next line crash site બને છે. તે BCA403 ના Unit 3 ની exception discipline છે જે callback clothing માં reborn થઈ છે: errors અહીં values છે, તમારી પાસે handed, અને handed error ને ignore કરવું એ Node માં one unforgivable style crime છે.

Quiz

registrations.txt માં 40 entries છે. committee fs.writeFile('registrations.txt', 'Riya: Garba Night', cb) ને run કરે છે એક more ને add કરવાના ઇરાદા સાથે. file માં પછી શું છે?

  1. 41 entries: end પર new one
  2. ફક્ત Riya ની single entry: writeFile એ આખું content ને REPLACES કર્યું
  3. 41 entries: top પર new one
  4. Error: writeFile existing files ને touch કરવા refuse કરે છે
Show the answer

ફક્ત Riya ની single entry: writeFile એ આખું content ને REPLACES કર્યું

writeFile એ whiteboard eraser છે: file જે કંઈ hold કરતી હતી તે gone છે, exactly new text દ્વારા replaced: 40 registrations એક wrong verb દ્વારા destroyed. ADDING એ appendFile નું job હતું, અને આ add-vs-replace pair એ exam નો favourite fs question છે અને genuinely dangerous real-world slip પણ (file પર કોઈ undo નથી). Option D safety interlock ને imagine કરે છે જે exist નથી કરતું: writeFile happily overwrites કરે છે. habit: write verb ને type કરતા પહેલા, પૂછો કે હું adding છું કે replacing?, અને answer ને method ને pick કરવા દો.

Think first

print order ને predict કરો

listing માં, appendFile ને readFile પહેલાં call કરવામાં આવે છે. શું 'Registration saved' એ 'So far:' પહેલાં print થવાની guarantee છે? યાદ કરો કે કયો model fs નું all run કરે છે, પછી answer પર commit કરો અને tap કરો.

Show the answer

Not guaranteed. બંને calls asynchronous છે: દરેક તેના disk work ને start કરે છે અને immediately return થાય છે; callbacks fire થાય છે જ્યારે તેમની I/O complete થાય છે, whichever order માં disk deliver કરે છે: code ની top-to-bottom order callbacks ની order વિશે કંઈ promise નથી કરતી. (તે પણ એટલે કે readFile append land થાય તે પહેલાં run થઈ શકે છે: possibly newest entry ને missing કરતું.) work જે write પર DEPEND કરે છે તે write ના callback ની અંદર nests થાય છે: append complete થાય તે પછી read ત્યાં nesting કરીને. async model નું third telling: effects, AJAX, હવે fs: એક model, આખો subject.

Watch out

File-handling slips

જ્યારે appendFile ને meant કર્યું હોય ત્યારે writeFile: 40-registrations wipe: add vs replace, હંમેશા consciously chosen.

err check ને Skipping કરવું: missing file તમને પછી one line later પર undefined data પર crash કરે છે, real cause થી far.

line order દ્વારા Sequencing કરવું: async callbacks તમારા file ની top-to-bottom poetry ને ignore કરે છે: dependencies callbacks ની અંદર nest થાય છે.

Paths relative છે જ્યાં node ran કર્યું: same app.js જે another folder થી started થાય છે તે ત્યાં READ અને WRITE કરે છે.

Theory

FestConnect Live, complete

આખા subject ને once walk કરો: data એ પોતાને describe કરવાનું શીખ્યું (XML), page ને move કરવાનું શીખ્યું (jQuery), data slimmed down થયું (JSON), browser ને reload વગર fetch કરવાનું શીખ્યું (AJAX), અને server ને exist કરવાનું, queries ને answer કરવાનું અને હવે REMEMBER કરવાનું શીખ્યું (Node + fs). એક coherent app, એક language end to end. BCA503-01 આ stack ને next year further લઈ જાય છે; async model અને err-first handshake તમારી સાથે જાય છે જ્યાં પણ JavaScript does.

Summary

Key takeaways

  • require('fs') File System module ને open કરે છે; દરેક operation asynchronous છે err-first callback સાથે.
  • Read: readFile(path, 'utf8', cb(err, data)); data ને touch કરતા પહેલા err ને check કરો.
  • Create: appendFile, open(path, 'w') અને writeFile બધા missing files ને create કરે છે.
  • Update: appendFile end પર ADDS કરે છે; writeFile everything ને REPLACES કરે છે: consciously choose કરો.
  • unlink સાથે Delete કરો; rename (અને move) rename(oldPath, newPath) સાથે કરો.
  • Callback order I/O completion ને follow કરે છે, code order નહીં: dependent work callbacks ની અંદર nests થાય છે.
  • Memory hook: append adds કરે છે, write wipes કરે છે, err first બોલે છે.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Node.js

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

File System Module: Read Files (readFile()), Create Files (appendFile(), open(), writeFile()), Update Files (appendFile(), writeFile()), Delete Files (unlink()), Rename Files (rename()) · Web Designing-2 (option A) · Gri-Learn