How to send email in Node.Js?

Hey guys,

Today I want to showย you how to use nodemailer in node.js to send email.

Node.js has a very active community that are adding new modules daily to the npm (node package module or node package manager), one my favorite module in node.js is nodemailer, this module helps you to send email to any recipient that you want, these are the steps that you need to take in order to use this module.

  1. You must install nodemailer as it is not a core module in node.js
Npm install nodemailer
  1. Create a object from the module.
const nodemailer = require('nodemailer');


  1. Create function that sends email and code all the configurations in this function:
<pre>function sendEmail() {
// you need to create an object that contains all the sender email configuration
  var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
      user: '',
      pass: ' you password '
// You need to create an email content
  var mailOptions = {
    from: '',
    to: '',
    subject: 'Your order is ready',
    text: 'Your order is ready for delivery',
    html: 'Your order is ready for delivery'
//Pass the email content to transporter in order to send email.
  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
    } else {
      console.log('Message Sent: ' + info.response);

What is connect module and how it works in Node.js

Hey guys,

I hope you are good, I am fantastic today ๐Ÿ™‚ ย I got my first mechanical keyboard, I feel awesome about it ๐Ÿ™‚

Today I have very interesting topic for you. I will show you how to work with connect module and how to process response and request. I hope you enjoy it ๐Ÿ™‚




What is Nitrous and How can code on browser?

Hey guys,

I just uploaded a video on my channel, I tired to explained about one of the coolest cloud service that I have seen, it helps you to code on PHP,Node.Js and so many other languages just on your browser without installing any extra tools or application on your machine.

enjoy the video.

How can create Module in Node.js and reference them in any js file?

Hey guys,

Node.js same as other programming languages supports module. Basically modules help us to have better structure for our application and also it let you to maintain your application easily. For example you have an application that are doing these tasks:

1. Send email
2. Grab files from FTP
3. Create output files and upload into FTP
Each of these tasks can be a separate module, for those that are familiar with object oriented programing, module can play the same role as class. Module is not something new in Node.js but you must know how to define module and how import them to your app.js or anywhere that you want to use them.
Follow these few steps:
1. Create a js file in root folder. ( you need to have node folder, if you do not know what is node folder you can first look at this post)
2. Each module can have two types of function.
2.1. Public function which others can call this function and it is like interface for the module.
2.2. Private function which is used by other functions inside the module and others from outside cannot use it.
3. Copy can past these code inside the js file.

var name;

exports.getname = function(inputname){


exports.Printname = function(){


This is a very simple example of module, in this module we have a global variable which is name and two functions, first is getname which gets name and the other one is Printname which prints out the name. To make a function public we need to export it.

How can call a module?
It is very easy, first you need to import your module, in this example I want to import the module in app.js file but it can be imported in any other js file, based on your need. To impost any module you must use this command:

var testmodule = require("./getname.js");

require is something like import in or using in C#, if your module installed inside the node_modules folder, then you do not need to mention any path an straight you can name the module. But if your js is in the root then you can access to it thru this address:

(“./getname.js”) // Name can be anything based on your module name

โ€œRequireโ€ creates an object from the module, so after that you can access all exported function via that object:


I hope this example helps you to understand Node module better, Node.js is like a puzzle you need to spend time to understand the whole story, and it may not be easy as the environment and concept is a bit different with what we used to it.

you can download full set of code form my github :

for any questions you can contact me through my email :

bye bye

How can connect to the MongoDB through Node.js?

Hey guys,
Today I am going to show you how connect to MongoDB through Node.js. Please follow the below steps:
Note: for those that do not know how to install and use MongoDB please what this Video or ready this post
Note: I am using Webstorm to develop Node.js, you can install 30 days trail version and if you found it is good then buy it or you can use Code IDE which is an IDE that is provided by Microsoft and it is free ๏Š
If you use Webstorm, it creates everything for you and you only need to change the code and no need to be worry about the packages and other settings that normally needed for Node.js, here is my Webstorm screen:

There are different libraries that can help you to connect to MongoDB, for this tutorial I am using monk, which is simple and very light.
1. Add these few lines of code to app.js file :

var mong = require('mongodb'); // we import MongoDB
// library to our project
var monk = require('monk');// we create an object 
//from monk liberay in order to connect to MongoDb
var db = monk('localhost:27017/userlist');// we create an database 
//object which is connected to our ruing MongoDB instance

2. In the next step we need to set db in all requests that come to our server, it is a bit different with what we have done in you must be aware that concept in Nodde.js is very different with what you know in or even Java.

req.db = db;

3. This few lines of code must be add before:

app.use('/', routes);
app.use('/users', users);

4. After these two simple steps, our app.js is ready to connect to MongoDB and gave service to rest of application. Now we need to add two functions to our route and create two more views in order to show the result and get data from user. Direct go to the route folder and open index.js and add these two functions:

Get data:

var db= req.db;
var collection=db.get('testcollection');





Receive post request and insert in database.'/adduser', function(req, res) {

// Set our internal DB variable
var db = req.db;

// Get our form values. These rely on the "name" attributes
var userName = req.body.username;
var userEmail = req.body.useremail;

// Set our collection
var collection = db.get('testcollection');

// Submit to the DB
"name" : userName,
"email" : userEmail
}, function (err, doc) {
if (err) {
// If it failed, return error
res.send("There was a problem adding the information to the database.");
else {
// And forward to success page

We need two different views for these two functions, To add view, straight go to the view folder, copy and paste index.jade and change name to userlist ‘and do the same and name it adduser.

Open userlist and copy this code there:

extends layout

block content
User List
each user, i in userlist

Open adduser and copy this code there:

extends layout
block content
h1= title
input#inputUserName(type="text", placeholder="username", name="username")
input#inputUserEmail(type="text", placeholder="useremail", name="useremail")
button#btnSubmit(type="submit") submit

I tried to explain everything in a short way. I will try to upload and video to show you everything.

Run your project and go to these two address to see your result:



Bye bye