November 20, 2020
Hello fellow devs š! This post contains the common and most frequently used JavaScript snippets. I am sure like me you have also found yourself in a situation where you are googling the most trivial stuff irrespective of your years of experience. To be honest, I still google how to parse Date
to show it in the desired way š¤.
Therefore, the idea behind writing this post is to document some of the commonly used code snippets that a regular developer encounters in his/her daily life. My intention is to write this sheet to serve two purposes -
Thereās a nice NPM module node-cron that lets us implement a cron Job very easily.
npm install node-cron
// Import module
const cron = require('node-cron');
// This expression defines the cron
const CRON_EXPRESSION = "*/10 * * * * *";
// Actual task to execute i.e., your business logic
const jobToExecute = () => {
console.log('Running a task every ten seconds');
}
// This function runs the job based on the CRON_EXPRESSION
const run = () => {
cron.schedule(CRON_EXPRESSION, () => {
jobToExecute();
});
}
Running a task every ten seconds
Running a task every ten seconds
Running a task every ten seconds
Running a task every ten seconds
Running a task every ten seconds
Running a task every ten seconds
Running a task every ten seconds
.
.
.
Below is a quick summary of how the cron expression is defined (source).
Reading from files is a common thing we do when we build an application using Node JS. Node provides a core module fs which lets us read file from the system easily.
We can read files in two ways - asynchronous (non-blocking) and synchronous (blocking). Usually the preferable way is non-blocking i.e., ask node to read a file, and then to get a callback when reading is finished.
// Import the module
const fs = require('fs');
// This function reads the file and prints the data on the console using the callback function
const readFile = () => {
fs.readFile('files/sample-text-file.txt', 'utf8', (err, data) => {
if (err) {
return console.log(err);
}
console.log(data);
});
}
module.exports = { readFile };
Here, we are passing the path of the file to be read and the type of encoding.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
If we wish to read the file synchronously then we can use the function readFileSync
instead of readFile
.
Like reading from file, a common use case is to write into file. We can leverage the core File System (fs) Node JS module.
// Import the core node js fs module
const fs = require('fs');
// The content to be written into the file
const contentToWrite = 'This content will be written into the file';
const writeFile = () => {
fs.writeFile('files/sample-file-to-write.txt', contentToWrite, (err) => {
if (err) {
throw err;
}
console.log('File is saved!');
});
}
module.exports = { writeFile };
File is saved!
Here we are writing the file asynchronously. To write into a file in synchronous fashion we can use writeFileSync
function instead of writeFile
.
So far we have seen reading to the file and writing to the file. Sometimes we need to delete a file. The core module of Node JS also allows us to do both synchronously and asynchronously.
// Import the core node js fs module
const fs = require('fs');
// Relative path of the file to be deleted
const fileName = 'files/sample-file-to-write.txt';
// This function deletes the file
const deleteFile = () => {
fs.unlink(fileName, (err) => {
if (err) {
throw err;
}
console.log("File deleted successfully");
});
}
module.exports = { deleteFile };
This will delete the file from the mentioned path. If we wish to use the file synchronously, then we can use fs.unlinkSync
instead of fs.unlink
.
We can easily send emails via the nodemailer module in Node JS. Below are the steps -
Install nodemailer module in your project using the following command -
npm install nodemailer
sendEmail.js
and paste the following code in it.const nodemailer = require('nodemailer');
const toEmail = '<recipient-email-id>';
const fromEmail = '<sender-email-id>';
const subject = '<subject-of-the-email>';
const message = '<message-of-the-email>';
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '<your-email-id>',
pass: '<your-password>'
}
});
const sendEmail = () => {
const details = {
from: fromEmail,
to: toEmail,
subject: subject,
html: message
};
transporter.sendMail(details, (error, data) => {
if (error) {
console.log(error);
} else {
console.log(data);
}
});
};
module.exports = {
sendEmail
};
nodemailer
module in our code. transport
object which will authenticate the sender. Here, you need to pass valid email address and password and the email client (GMail
in our case).transporter.sendEmail
function to send and email.NOTE: You need to allow āless secure appsā in GMail using the steps mentioned here.
This post is currently in āWork In Progressā. I will add more snippets to this post as and when I think of them. I would love if you awesome people also suggest some commonly used code snippets you encounter in your daily work.
As usual, you can find the complete code on my GitHub. If you find it useful, consider giving it a star ā.
Happy Learning š and Namaste š.