// 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.');
});Read and Write Files in Node.js using 'fs' module
Continue Reading
Discover more amazing content handpicked just for you
Build a Custom Rate Limiter in Node.js with Redis
After 100 requests within an hour, you’ll get:
{
"error": "Too many requests. Try later."
}Building a Real-Time Chat Application with WebSockets in Node.js
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.chat-container {
background-color: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
}
#messages {
list-style-type: none;
padding: 0;
max-height: 300px;
overflow-y: auto;
margin-bottom: 10px;
}
#messages li {
padding: 8px;
background-color: #f1f1f1;
border-radius: 3px;
margin-bottom: 5px;
}
#chat-form {
display: flex;
}
#message {
flex: 1;
padding: 8px;
border: 1px solid #ddd;
border-radius: 3px;
margin-right: 5px;
}
button {
background-color: #007bff;
color: white;
border: none;
border-radius: 3px;
padding: 8px 12px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}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);
});JWT Token Creation and Verification in Node.js using 'jsonwebtoken'
No preview available for this content.
Simple HTTP Server in Node.js
No preview available for this content.
Simple RESTful API in Node.js using Express
No preview available for this content.
Date Manipulation and Sum Calculation
// Import required modules
const moment = require('moment');
const _ = require('lodash');
// Get the current date and time using Moment.js
const now = moment();
// Example array of numbers
const numbers = [1, 2, 3, 4, 5];
// Calculate the sum of the array using Lodash
const sum = _.sum(numbers);Access Command-line Arguments
No preview available for this content.
Set and Access Environment Variable
No preview available for this content.
Event Emitter using 'events' module
No preview available for this content.
Construct File Path using 'path' module
No preview available for this content.
Basic Authentication using 'express-basic-auth' middleware
No preview available for this content.
Create and Print Buffer
// Create a Buffer from a string
const buffer = Buffer.from('Hello, Node.js!');
// Print the Buffer as a string
console.log('Buffer:', buffer.toString());Hashing Password with SHA-256 using 'crypto' module
No preview available for this content.
Parse URL and Query Parameters
No preview available for this content.
Execute Shell Command using 'child_process' module
No preview available for this content.
File Stream Copy using 'fs' module
No preview available for this content.
Simple WebSocket Server using 'ws' library
No preview available for this content.
JSON File Reading and Decoding
$jsonString = file_get_contents('data.json');
$data = json_decode($jsonString, true);
print_r($data);
Discussion 0
Please sign in to join the discussion.
No comments yet. Start the discussion!