How to add ics file events in calendar using nodejs
<p>There is a scenario where I need to send event meeting invites to end users. I am able to generate the ICS file and send it as attachment. But the ICS files are not readable or added in User Calander.</p>

<p>Code to generate and send email is as below:</p>

<pre><code>  var transport = require('nodemailer-smtp-transport'),
    transporter = nodemailer.createTransport(transport(config.mailer)),
    sendMail = function(mailOptions) {
      transporter.sendMail(mailOptions, function(err, response) {
        if (err) return err;
        return response;
      });
    },
    eventEmailToUser = function(user, events, createdBy, mailOptions) {
    var ical = require('ical-generator');
    var cal = ical();
      var username = user.username ? user.username : ' ';
      var eventName = events.title ? events.title : ' ';
      var eventDate = moment.tz(events.date, 'Asia/Kolkata');
      eventDate = eventDate.format('YYYY-MM-DD h:mm a');
      cal.addEvent({
        start: new Date(),
        end: new Date(new Date().getTime() + 3600000),
        summary: events.title,
        uid: events._id, // Some unique identifier
        sequence: 0,
        description: events.description,
        location: events.location,
        organizer: {
          name: createdBy.username,
          email: createdBy.email
        },
        method: 'request'
      });

      var path = '/files/' + events._id + '.ics';
      cal.save(path, function(err, file) {
        if (err) return err;
      });

      mailOptions.alternatives = [{
        contentType: "text/calendar",
        contents: new Buffer(cal).toString()
      }];

      mailOptions.attachments = [{
        filename: events.title + '.ics',
        filePath: path
      }];
      mailOptions.html = [
        '&lt;div&gt;',
        '&lt;div&gt;Hi &lt;b&gt;' + username + '&lt;/b&gt;&lt;br/&lt;br/&gt;',
        ' You have just confirmed to attend &lt;b&gt;' + eventName + '&lt;/b&gt; on &lt;b&gt;' + eventDate + '&lt;/b&gt;',
        ' &lt;br/&gt;&lt;br/&gt;',
        'Thanks',
        ' &lt;br/&gt;',
        '&lt;/div&gt;',
        '&lt;br/&gt;',
        '&lt;/div&gt;'
      ].join('\n\n');
      mailOptions.subject = 'Invitation for' + eventName;
      return mailOptions;
    };
  exports.sendInvite = function(req, res) {
    var userMailOptions = {
      to: 'abc@gmail.com',
      from: 'xyz@gmail.com',
    };
    userMailOptions = eventEmailToUser(user, events, eventCreator, userMailOptions);
    var userEmailresult = sendMail(userMailOptions);
  };
</code></pre>