Skip to content

Commit

Permalink
hua
Browse files Browse the repository at this point in the history
  • Loading branch information
aconanlai committed Oct 11, 2017
1 parent 5070a90 commit 9f22c9b
Show file tree
Hide file tree
Showing 8 changed files with 1,356 additions and 199 deletions.
10 changes: 10 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
"extends": "airbnb",
"env": {
"node": true
},
"rules": {
"strict": 0,
"no-var": 0
}
};
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
.DS_Store
.DS_Store

config.js
101 changes: 51 additions & 50 deletions api/api.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
var express = require('express');

var app = express();
var router = express.Router();
var https = require('https');
var nodemailer = require('nodemailer');
// var https = require('https');
// var nodemailer = require('nodemailer');

var server = require('./server.js');
/*
configuring our SMTP Server details.
STMP is mail server which is responsible for sending and recieving email.
*/
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "Your Gmail ID",
pass: "Gmail Password"
}
});
/*
configuring our SMTP Server details.
STMP is mail server which is responsible for sending and recieving email.
*/
var smtpTransport = nodemailer.createTransport("SMTP", {
service: "Gmail",
auth: {
user: "Your Gmail ID",
pass: "Gmail Password"
}
});

var rand,mailOptions,host,link;
var rand, mailOptions, host, link;

/*------------------SMTP Over-----------------------------*/
/*------------------SMTP Over-----------------------------*/

/*------------------Routing Started ------------------------*/

app.get('/send',function(req,res){
rand = Math.floor((Math.random() * 100) + 54);
host = req.get('host');
link="http://"+req.get('host')+"/verify?id="+rand;
mailOptions={
to : req.query.to,
subject : "Question",
html : "Hello,<br> Please Click on the link to verify your email.<br> <a href=" + link + ">Click here to verify</a>"
}
console.log(mailOptions);
smtpTransport.sendMail(mailOptions, function(err, response){
if(err){
console.log(err);
res.end("error");
}else{
console.log("Message sent: " + response.message);
res.end("sent");
}
});
});
app.get('/send', function (req, res) {
rand = Math.floor((Math.random() * 100) + 54);
host = req.get('host');
link = "http://" + req.get('host') + "/verify?id=" + rand;
mailOptions = {
to: req.query.to,
subject: "Question",
html: "Hello,<br> Please Click on the link to verify your email.<br> <a href=" + link + ">Click here to verify</a>"
}
console.log(mailOptions);
smtpTransport.sendMail(mailOptions, function (err, response) {
if (err) {
console.log(err);
res.end("error");
} else {
console.log("Message sent: " + response.message);
res.end("sent");
}
});
});

app.get('/verify',function(req,res){
console.log(req.protocol + ":/" + req.get('host'));
if((req.protocol+"://"+req.get('host'))==("http://"+host))
{
app.get('/verify', function (req, res) {
console.log(req.protocol + ":/" + req.get('host'));
if ((req.protocol + "://" + req.get('host')) == ("http://" + host)) {
console.log("Domain is matched. Information is from Authentic email");
if(req.query.id==rand){
console.log("email is verified");
res.end("<h1>Email "+mailOptions.to+" has been Successfully verified");
}
else{
console.log("email is not verified");
res.end("<h1>Bad Request.</h1><p>Email is not verified, does not work, or just doesn't exist. Try again.</p>");
}
if (req.query.id == rand) {
console.log("email is verified");
res.end("<h1>Email " + mailOptions.to + " has been Successfully verified");
}
else { res.end("<h1>Request is from unknown source");
else {
console.log("email is not verified");
res.end("<h1>Bad Request.</h1><p>Email is not verified, does not work, or just doesn't exist. Try again.</p>");
}
}
else {
res.end("<h1>Request is from unknown source");
}
});

/*--------------------Routing Over----------------------------*/
module.exports = router;
/*--------------------Routing Over----------------------------*/
module.exports = router;
24 changes: 24 additions & 0 deletions api/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var express = require('express');
var bodyParser = require('body-parser');
var dB = require('./mongo');
var app = express();

var questionsRouter = require('./routes/questions');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// respond with "hello world" when a GET request is made to the homepage
app.get('/', (req, res) => {
res.send('hello world');
});

questionsRouter.get('/', (req, res) => {
res.send('send all the questions with responses here');
});

app.use('/questions', questionsRouter);

dB.start();

app.listen('3000');
23 changes: 23 additions & 0 deletions api/mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var config = require('../config');
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');

// Connection URL
var url = config.mongoUrl;
// Use connect method to connect to the Server

var db = null;

module.exports = {
start: () => {
MongoClient.connect(url, (err, database) => {
if (err) {
console.log(`err: ${err}`)
} else {
console.log("Connected correctly to server");
db = database;
}
});
},
get: () => db,
};
25 changes: 25 additions & 0 deletions api/routes/questions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var express = require('express');
var dB = require('../mongo');
var questionsRouter = express.Router();

questionsRouter.get('/', (req, res) => {
res.send('send all the questions with responses here');
});

questionsRouter.post('/ask', (req, res) => {
console.log('question received');
console.log(req.body);
const db = dB.get();
const collection = db.collection('questions');
collection.insert(req.body, (err, result) => {
if (err) {
console.log(`err: ${err}`);
} else {
console.log('inserted');
console.log(result);
}
});
res.send('you sent a question');
});

module.exports = questionsRouter;
Loading

0 comments on commit 9f22c9b

Please sign in to comment.