Mail API with Node.js, Express

Creating the Application:

1. Fire up your terminal and create a new folder for the application.

 mkdir node-api

2. Initialize the application with a package.json file

Go to the root folder of your application and type npm init to initialize your app with a package.json file.

 cd node-api
 npm init
name: (node-api) 
version: (1.0.0) 
description: Restful API using Node js
entry point: index.js
test command: 
git repository: 
keywords: Express RestAPI MongoDB Mongoose Notes
author: Tapan Kumar Acharjee
license: tapu_0025


{
  "name": "node-api",
  "version": "1.0.0",
  "description": "Restful API using Node js",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "Express",
    "RestAPI",
    "MongoDB",
    "Mongoose",
    "Notes"
  ],
  "author": "Tapan Kumar Acharjee",
  "license": "tapu_0025"
}

Is this ok? (yes) yes

Note that I’ve specified a file named index.js as the entry point of our application. We’ll create index.js file in the next section.

3. Install dependencies

We will need express, mongoose and body-parser modules in our application. Let’s install them by typing the following command –

npm install express  --save

npm install body-parser --save

I’ve used --save option to save all the dependencies in the package.json file. The final package.json file looks like this –

{
  "name": "node-api",
  "version": "1.0.0",
  "description": "Restful API using Node js",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "Express",
    "RestAPI"
  ],
  "author": "Tapan Kumar Acharjee",
  "license": "tapu_0025",
  "dependencies": {
    "body-parser": "^1.18.2",
    "express": "^4.16.3"
  }
}

Our application folder now has a package.json file and a node_modules folder –

node-api
    └── node_modules/
    └── package.json

Setting up the web server

var express = require("express");
var app = express();
var nodemailer = require('nodemailer');
var port = 3000;

//middleware start here

var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended:true
}));

//middleware end

//Api for send mail start here

app.post('/sendmail', function(req, res, next) {

nodemailer.createTestAccount((err, account) => {
if (err) {
console.error('Failed to create a testing account. '+err.message);
returnprocess.exit(1);
}

var transporter= nodemailer.createTransport({
  service:'gmail',
  auth: {
     user:'xxxxx@gmail.com',
     pass:'xxxx'
   }
});
var mailOptions= {
from:'xyz@gmail.com',
to:'tapanspectrum@gmail.com',
subject:'mail testing',
text:'Hello to myself!',
html:'<p><b>Hello</b> to myself!</p>'

};
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
console.log('Error occurred. '+err.message);
returnprocess.exit(1);
} else {
varoMyOBject= {
"Status":"Success",
"message":"Send Successfully",
"result":info.messageId  // return mail id
};
res.json(oMyOBject);
}

});

});

});

//Api for send mail end here

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2024 TapanAcharjee

Up ↑