javascript - nodejs - handling files

nodejs - handling files

super basic and old school

Read a file in Node.js -

var fs = require('fs'),
    path = require('path'),    
    filePath = path.join(__dirname, 'start.html');

fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
    if (!err) {
        console.log('received data: ' + data);
        response.writeHead(200, {'Content-Type': 'text/html'});
    } else {


fs.readFile vs streams to read text files in node.js - - Duncan Grant - 20170822

TLDR: my suggestion is once you are planning on dealing with text files of greater size than around 10MB, it’s best to ditch readFile and start using streams instead.

with promises

From callbacks to fs/promises to handle the file system in Node.js - - 20180529

In this post, I will show you the evolution of Node.js to work with the file system.

With callback (old school way to do in node) :

const fs = require("fs");

fs.writeFile("/tmp/test.js", "console.log('Hello world');", error => {
    if (error) console.error(error);
    else console.log("file created successfully!");

Promisify must be manual before node@8 (or using bluebird or Q) :

const fs = require("fs");

const writeFilePromise = (file, data) => {
    return new Promise((resolve, reject) => {
        fs.writeFile(file, data, error => {
            if (error) reject(error);
            resolve("file created successfully with handcrafted Promise!");

        "console.log('Hello world with handcrafted promise!');"
    .then(result => console.log(result))

    .catch(error => console.log(error));

The node@8 release introduce util.promisify() api :

const fs = require("fs");
const util = require("util");

const writeFile = util.promisify(fs.writeFile);

writeFile("/tmp/test3.js", "console.log('Hello world with promisify!');")
  .then(() => console.log("file created successfully with promisify!"))

  .catch(error => console.log(error));

Same with async/await :

const fs = require("fs");
const { promisify } = require("util");

const writeFile = promisify(fs.writeFile);

async function main() {
    await writeFile("/tmp/test4.js",
        "console.log('Hello world with promisify and async/await!');");"file created successfully with promisify and async/await!");

main().catch(error => console.error(error));

The node@10 release introduce fs/promises api :

const fsp = require("fs").promises;

try {
    await fsp.writeFile("/tmp/test5.js", "console.log('Hello world with Node.js v10 fs/promises!'");"File created successfully with Node.js v10 fs/promises!");
} catch (error){

This code is using the experimental top level await feature, node needs to be executed with the --experimental-repl-await flag.

results for ""

    No results matching ""