NodeJS – CodingNepal https://www.codingnepalweb.com CodingNepal is a blog dedicated to providing valuable and informative content about web development technologies such as HTML, CSS, JavaScript, and PHP. Thu, 11 May 2023 07:45:18 +0000 en-US hourly 1 https://wordpress.org/?v=6.4.2 PHP vs Node.js – Which is best for Backend Development? https://www.codingnepalweb.com/php-vs-nodejs-which-is-best-for-backend/ https://www.codingnepalweb.com/php-vs-nodejs-which-is-best-for-backend/#comments Tue, 27 Dec 2022 11:48:18 +0000 https://www.codingnepalweb.com/?p=3736 PHP vs Node.js Which is best for Backend Development

There are numerous programming languages available for backend development, but two of the most commonly used are PHP and Node.js. These languages are popular choices for building backend web applications and websites, but they each have their own unique set of strengths and weaknesses.

To help you decide which technology is the best fit for your backend development project, this blog will provide an overview of PHP and Node.js. We’ll cover what these languages are, what they are used for, and the critical differences between them.

By understanding the capabilities and features of each technology, you can make an informed decision about which one is the best fit for your specific project needs and requirements.

But before continuing this blog, there is also a lot of confusion among frontend developers when choosing the best framework for frontend development, if you’re one of them, this blog might be helpful to you: React vs Angular – Which is best for Frontend Development?

What is PHP, and what does it do?

What is PHP, and what does it do

PHP, which stands for “Hypertext Preprocessor,” is a server-side scripting language that is widely used for creating dynamic web applications and websites. It is executed on the server and generates HTML (HyperText Markup Language), which is then sent to the client’s web browser.

PHP was developed by Rasmus Lerdorf in 1994 and has since become a popular choice for building dynamic web pages. It is well-known for its ease of use, and it has a large ecosystem of frameworks such as Laravel, CodeIgniter, Symfony, etc., which offer a range of tools and features to help developers build robust and scalable web applications easily.

PHP has numerous advantages, some of which are listed below:

  • It is easy to learn and use even if you’re a newbie in backend development.
  • It has a large ecosystem of powerful libraries and frameworks available.
  • It is compatible with a wide range of platforms including Linux, and Windows.
  • It has built-in support for popular databases that make data easy to read and store.
  • It has many built-in security features such as input validation and sanitization.
  • It has a huge and active community of developers.

What is Node.js, and what does it do?

What is Node.js, and what does it do

Node.js is a JavaScript runtime environment that enables the execution of JavaScript code on the server side, outside of a web browser. It was developed by Ryan Dahl in 2009 and built using the V8 JavaScript engine, which is also used by the Chrome web browser.

Node.js is designed to be lightweight, efficient, and well-known for its fast performance and scalability. As a result, it is ideal for creating real-time web applications such as chat applications and online games. Node.js is also a cross-platform technology, which means it can be used on various operating systems, including Windows, macOS, and Linux.

Node.js has numerous advantages, some of which are listed below:

  • It is easy to learn and use, especially if you’re familiar with JavaScript.
  • It is efficient and well-suited for building real-time applications and APIs.
  • It uses an event-driven, non-blocking I/O model that makes it asynchronous.
  • It is a cross-platform technology that works on a variety of platforms.
  • It can be used for both the front-end and back-end of web development.

Differences between PHP and Node.js

There are several differences between PHP and Nod.js. But some key differences between them are pointed out below.

PHP Node.js
It is a server-side scripting language. It is a JavaScript runtime environment.
It uses a traditional synchronous programming model. It uses an event-driven, non-blocking I/O model.
It has a large ecosystem of libraries and frameworks. It has a more limited set of tools and libraries.
It is slower and less efficient than Node.js. It is faster and more efficient than PHP.
It is used for server-side rendering of web pages. It is used for building server-side applications and APIs.
It is used in collaboration with relational databases like MySQL. It works perfectly with NoSQL (not only SQL) databases like MongoDB.

Which should you use, PHP or Node.js?

As you are now aware, what are PHP and Node.js, their pros and cons, and the differences between them? The decision of which technology you should use will depend on your specific project requirements and the resources and skills that are available to you.

PHP can be the best choice if you are a newbie and want to build a simple, traditional web application or website because it is easy to learn and has a large number of frameworks. But if you are familiar with JavaScript and want to build real-time, high-performance web applications or APIs, then Node.js might be a better choice for you.

Ultimately, each technology has its own advantages and disadvantages. So, before selecting any of these technologies for your backend development, it is important to consider your project requirements, objectives, and your personal interests and skills.

If you found this blog useful, please consider sharing it with others to help them benefit as well. Thank you for your support and keep visiting our site.

]]>
https://www.codingnepalweb.com/php-vs-nodejs-which-is-best-for-backend/feed/ 5
How to Send Email in Node.js using Nodemailer & Gmail https://www.codingnepalweb.com/how-to-send-email-in-nodejs-and-nodemailer/ https://www.codingnepalweb.com/how-to-send-email-in-nodejs-and-nodemailer/#respond Sun, 06 Nov 2022 14:49:20 +0000 https://www.codingnepalweb.com/?p=3136 How to Send Email in Node.js using Nodemailer & Gmail

As a part of experiments, developers need to send emails from their Node.js application for different purposes, and it looks like a complex task until they know the easy way to do it. So, if you are looking for an easy and straightforward way to send emails from your NodeJS app, this blog is written for you.

In this blog, you’ll learn how to Send Emails in Node.js using Nodemailer and Gmail. If you don’t know, Nodemailer is a module for Node.js applications widely used to send emails.

There are many modules and ways to send email in NodeJS like SendGrid, but I’ll use Nodemailer and Gmail among them because they are easy to set up to send mail.

As you know, Google removed the “Less secure apps” feature on May 30, 2022. So, now it is a little more difficult to send mail using Gmail in Node.js app than before but don’t worry at the end of this blog you’ll be able to send mail from the Node.js app using Nodemailer and Gmail.

Steps to Send Email in Node.js using Nodemailer

  1. Create a folder with any name and open it in your VS Code.
  2. Open the terminal by pressing ctrl + j and run this command npm init -y to create a package.json file for a new node.js app.
  3. Install the Nodemailer module using this command npm i nodemailer
  4. Create an app.js file and paste the given codes.
const nodemailer = require("nodemailer");

const sendEmail = async (mailDetails) => {
    const transporter = nodemailer.createTransport({
        host: "smtp.gmail.com",
        port: 587,
        auth: {
            user: "youremail@site.com",
            pass: "your-password",
        },
        tls: {
            rejectUnauthorized: false
        }
    });

    try {
        console.log("Sending your email...");
        await transporter.sendMail(mailDetails);
        console.log(`Email sent successfully to ${mailDetails.to}`);
    } catch (error) {
        console.log("Sorry, failed to send your email!");
    }
}

sendEmail({
    from: "sender-email-address",
    to: "receiver-email-address",
    subject: "Test Email via NodeJS using Nodemailer",
    text: "Hi, there...This is a test email sent via NodeJS App using Nodemailer."
});

If you look at line.no 7, there you need to pass your Google Account email and the Google App password as a user and pass value. Before May 30, 2022, when the “Less secure app” feature is allowed by Google, there you can pass your Google Account password to send an email. But, now you’ve to pass the App password.

To create the App Password in your Google Account, go to manage your google account > Click Security > Under “Signing in to Google” select App passwords > Select App & Device. Now you’ll get a 16-character code and this is your app password. For more details about creating App Passwords, read the Official Google Article.

Once you have done the above steps, go to line.no 26 and pass your email as from and receiver email as to value. At last, open the terminal and run this command node app.js to run your node.js app to send an email.

Conclusion

In this blog, you’ve learned how to Send an Email in Node.js applications using Nodemailer and Gmail. Remember, not only for experiments, but you can use Nodemailer in production too. If you want to know how to Send Emails in PHP from Localhost using Gmail. You can view this blog.

If you couldn’t send an email or found any problem, don’t hesitate to comment down. If you found this blog helpful, don’t forget to share it with others. Happy Coding!

]]>
https://www.codingnepalweb.com/how-to-send-email-in-nodejs-and-nodemailer/feed/ 0