-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
149 lines (126 loc) · 3.76 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const express = require('express')
const morgan = require('morgan')
const bodyparser = require('body-parser')
const cors = require('cors')
const app = express()
const mongoose = require ('mongoose')
const sgMail = require('@sendgrid/mail');
const transaction = require('./models/certauth.model');
const { errorHandler } = require('./helpers/dbErrorHandling')
//Config .env to ./config/config.env
require('dotenv').config({
path:'./config/config.env'
})
//Connect to Database
const uri = process.env.MONGO_URI
mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
}
);
const connection = mongoose.connection;
connection.once('open', () => {
console.log ("MongoDB database connection established successfully");
})
//Use bodyParser
app.use(bodyparser.json())
//Load all routes
const authRouter = require('./routes/auth.route')
const userRouter = require('./routes/user.route')
//config for only development
if(process.env.NODE_ENV === 'development') {
app.use(cors({
origin: process.env.CLIENT_URL
}))
app.use(morgan('dev'))
//Morgan give information about each request
//Cors it's allow to deal with react for localhost at port 3000 without any problem
}
//Use Routes
app.use('/api/',authRouter);
app.use('/api', userRouter);
//client-mail
app.post('/api/smail', (req, res, next) => {
sgMail.setApiKey(process.env.MAIL_KEY);
//console.log(req.body)
//console.log(req.body.z)
const email = req.body.z
const emailData = {
to: email,
from: process.env.EMAIL_FROM,
fromname: 'SADG University',
subject: 'Graduation Certificate',
html: `
<h1>Greetings from SADG University</h1>
<br>
<h2>Hello ${req.body.y}</h1>
<br>
<span style="font-weight: 10px">This is your certificate id : <strong>${req.body.x}</strong></span>
<br>
<a href="https://sadg-university.herokuapp.com/">Click here to view Certificate</a>
<br>
<h2>Management, Principal, Faculty Congratulates you</h2>
<br>
<h3> We wish you All the Best for your Future Endeavours</h3>
<
`,
};
sgMail.send(emailData).then(sent => {
return res.json({
message: `Email has been sent to ${email}`
});
}).catch(err => {
return res.status(400).json({
success: false
});
});
const tid = req.body.t
const usn = req.body.u
const cert_id = req.body.x
console.log(tid)
const certx = new transaction({
usn,
tid,
email,
cert_id
});
certx.save((err, certx) => {
if (err) {
console.log('Save error', errorHandler(err));
return res.status(401).json({
errors: errorHandler(err)
});
} else {
return res.json({
success: true,
message: 'Transaction details sent!!',
});
}
});
})
app.post('/api/idfetch', (req, res, next) => {
const usn = req.body.id
//console.log(usn)
transaction.findOne({
usn
}).exec((err, tid) => {
return res.send(tid)
})
});
app.get('/api/getrapi', (req, res, next) => {
return res.send(process.env.INFURA_API_KEY)
});
app.get('/api/rdefault', (req, res, next) => {
return res.send(process.env.ROPSTEN_DEFAULT_ACCOUNT)
});
app.use((req, res, next) => {
res.status(404).json({
success: false,
message: "Page not found"
})
});
const PORT = process.env.PORT
var listener = app.listen(PORT, function() {
console.log(`App listening on port ${PORT}`); //Listening on port 8888
});