This repository has been archived by the owner. It is now read-only.
Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
gallery/routes2.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
71 lines (61 sloc)
2.22 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable max-lines-per-function */ | |
//#!/usr/bin/env node | |
//Routes File | |
'use strict' | |
/* MODULE IMPORTS */ | |
const Koa = require('koa') | |
const views = require('koa-views') | |
const staticDir = require('koa-static') | |
const bodyParser = require('koa-bodyparser') | |
const session = require('koa-session') | |
const nodemailer = require('nodemailer') | |
/* IMPORT CUSTOM MODULES */ | |
const Display = require('./modules/display.js') | |
const app = new Koa() | |
const router = require('./routes') | |
/* CONFIGURING THE MIDDLEWARE */ | |
app.keys = ['darkSecret'] | |
app.use(staticDir('public')) | |
app.use(bodyParser()) | |
app.use(session(app)) | |
app.use(views(`${__dirname}/views`, { extension: 'handlebars' }, {map: { handlebars: 'handlebars' }})) | |
router.post('/emailConfirmation', async ctx => { | |
try { | |
const display = await new Display('website.db') | |
console.log('yo') | |
const body = ctx.request.body | |
const emailInfo = await display.emailDetails(ctx.session.item, ctx.session.username) | |
console.log('passou o method') | |
const smtpConfig = { | |
host: 'smtp.gmail.com', | |
port: 465, | |
secure: true, // use SSL | |
auth: { | |
user: 'lewtestmailcs@gmail.com', | |
pass: 'ilolpop123' | |
} | |
} | |
const transporter = nodemailer.createTransport(smtpConfig) | |
console.log(emailInfo) | |
const mailOptions = { | |
from: 'Lewis Test Mail <lewistestmailcs@gmail.com>', | |
to: emailInfo.email.email, | |
subject: 'Someone is interested in your item', | |
text: `${ctx.session.username} is interested in your item "${emailInfo.title}", | |
priced at ${emailInfo.price}. | |
${ctx.session.username} contact information: | |
Email: ${emailInfo.sender.email}, Phone: ${emailInfo.sender.phone} | |
message: ${body.message}`, | |
html: `<p>${ctx.session.username} is interested in your item "${emailInfo.title}", | |
priced at ${emailInfo.price}.</p> | |
<p>${ctx.session.username} contact information:</p> <p>Email: ${emailInfo.sender.email}</p> | |
<p>Phone: ${emailInfo.sender.phone}</p> | |
<p> message: ${body.message}</p>` | |
} | |
await transporter.sendMail(mailOptions) | |
await ctx.render('emailConfirmation', {detailsmsg: {msg: 'Email has been sent!'}}) | |
} catch(err) { | |
await ctx.render('error', {detailsmsg: err}) | |
} | |
}) | |
module.exports = router |