DeveloperBreeze

Read and Write Files in Node.js using 'fs' module

// Import 'fs' module
const fs = require('fs');

// Read a file
fs.readFile('file.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

// Write to a file
fs.writeFile('newfile.txt', 'Hello, Node.js!', (err) => {
  if (err) throw err;
  console.log('File written successfully.');
});

Continue Reading

Discover more amazing content handpicked just for you

Tutorial

Build a Custom Rate Limiter in Node.js with Redis

  • Offer different limits for free vs paid users
  • Log or monitor usage per user

This isn’t just a quick fix—it’s a deep dive into:

Apr 04, 2025
Read More
Tutorial
javascript css +1

Building a Real-Time Chat Application with WebSockets in Node.js

const socket = io();

const messageForm = document.getElementById('chat-form');
const messageInput = document.getElementById('message');
const messagesList = document.getElementById('messages');

messageForm.addEventListener('submit', (e) => {
    e.preventDefault();
    const message = messageInput.value;
    socket.emit('chatMessage', message);
    messageInput.value = '';
});

socket.on('chatMessage', (message) => {
    const li = document.createElement('li');
    li.textContent = message;
    messagesList.appendChild(li);
});
node server.js

Aug 03, 2024
Read More
Code
php

JWT Token Creation and Verification in Node.js using 'jsonwebtoken'

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Simple HTTP Server in Node.js

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Simple RESTful API in Node.js using Express

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Date Manipulation and Sum Calculation

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Access Command-line Arguments

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Set and Access Environment Variable

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Event Emitter using 'events' module

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Construct File Path using 'path' module

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Basic Authentication using 'express-basic-auth' middleware

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Create and Print Buffer

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Hashing Password with SHA-256 using 'crypto' module

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Parse URL and Query Parameters

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Execute Shell Command using 'child_process' module

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

File Stream Copy using 'fs' module

// Import 'fs' module
const fs = require('fs');

// Create read and write streams
const readStream = fs.createReadStream('input.txt');
const writeStream = fs.createWriteStream('output.txt');

// Pipe data from read stream to write stream for file copy
readStream.pipe(writeStream);

Jan 26, 2024
Read More
Code
javascript

Simple WebSocket Server using 'ws' library

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Read JSON Data from a File

No preview available for this content.

Jan 26, 2024
Read More
Code
php

JSON File Reading and Decoding

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!