forked from yogdaan/blockchain-certification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
115 lines (96 loc) · 2.8 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
require("dotenv").config();
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const log = require("./utils/log");
const path = require("path");
if (process.env.NODE_ENV === undefined) process.env.NODE_ENV = "development";
const Certificates = require("./model/Certificates");
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
// logger
app.use((req, res, next) => {
const now = new Date().toString().slice(4, 24);
res.on("finish", () => {
log.Logger(`${now} ${req.method} ${res.statusCode} ${req.url}`);
});
next();
});
// CORS
if (process.env.NODE_ENV !== "production") app.use(require("cors")());
app.get("/certificate/data/:id", (req, res) => {
let certificateId = req.params.id;
Certificates.findById(certificateId)
.then(obj => {
if (obj === null)
res.status(400).send({ err: "Certificate data doesn't exist" });
else res.send(obj);
})
.catch(err => res.status(400).send({ err }));
});
app.get("/certificate/verify/:id", (req, res) => {
let certificateId = req.params.id;
Certificates.findById(certificateId)
.then(obj => {
obj.verifyData().then(verified => {
if (verified) res.status(200).send();
else res.status(401).send();
});
})
.catch(err =>
res.status(400).send({ err: "No data found for the given certificateId" })
);
});
app.post("/certificate/generate", (req, res) => {
const { candidateName, orgName, courseName, assignDate, duration } = req.body;
const given = new Date(assignDate);
let expirationDate = given.setFullYear(given.getFullYear() + duration);
expirationDate = expirationDate.toString();
const certificate = new Certificates({
candidateName,
orgName,
courseName,
expirationDate,
assignDate,
duration
});
certificate
.save()
.then(obj => {
const dbRes = obj.toJSON();
obj
.appendBlockchain()
.then(data => {
const { transactionHash, blockHash } = data.receipt;
res.status(201).send({
receipt: {
transactionHash,
blockHash
},
data: dbRes
});
})
.catch(err => res.status(500).send(err));
})
.catch(err => {
log.Error(err);
res.status(400).send();
});
});
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
const port = process.env.PORT || 3000;
app.listen(port, () => {
log.Info(
`This is a ${
process.env.NODE_ENV
} environment.\nServer is up on port ${port}`
);
});
module.exports = { app };