-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
129 lines (118 loc) · 3.32 KB
/
server.js
File metadata and controls
129 lines (118 loc) · 3.32 KB
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
const express = require("express");
const mongoose = require("mongoose");
const axios = require("axios");
const web3 = require("web3");
const path = require("path");
require("dotenv").config();
const {
ensureOnlyOneListing,
ensureUserOwnsListing,
} = require("./middleware/middleware");
const app = express();
// TURN OFF IN PRODUCTION
// const allowedOrigins = ["http://localhost:3000", "http://localhost:5000"];
// const cors = require("cors");
// app.use(
// cors({
// origin: function (origin, callback) {
// if (!origin) return callback(null, true);
// if (allowedOrigins.indexOf(origin) === -1) {
// const msg =
// "The CORS policy for this site does not allow access from the specified Origin.";
// return callback(new Error(msg), false);
// }
// return callback(null, true);
// },
// })
// );
app.use(express.json());
// Connect to MongoDB
const db = require("./config/keys").mongoURI;
mongoose
.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("MongoDB Connected"))
.catch((err) => console.log(err));
const Listing = require("./models/Listing");
const port = process.env.PORT || 5000;
app.use("/static", express.static("public"));
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, "build")));
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
app.post("/new", ensureOnlyOneListing, (req, res) => {
const { userAddress, name, description, goalAmount } = req.body;
console.log(name);
const newListing = new Listing({
userAddress,
name,
description,
goalAmount,
});
newListing.save().then((listing) => {
res.send(listing);
});
});
app.post("/crowdfund/:userAddress", (req, res) => {
const userAddress = req.params.userAddress;
Listing.findOne({ userAddress: userAddress }, (err, listing) => {
if (err) {
console.log(err);
} else {
res.send(listing);
}
});
});
app.put("/crowdfund/:userAddress", (req, res) => {
axios
.post(
`https://mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`,
{
jsonrpc: "2.0",
method: "eth_getTransactionByHash",
params: [req.body.transactionHash],
id: 1,
},
{
headers: {
"Content-Type": "application/json",
},
}
)
.then((x) => {
if (x.data.result === null) {
} else {
const userAddress = req.params.userAddress;
Listing.findOne({ userAddress: userAddress }, (err, listing) => {
if (err) {
console.log(err);
} else {
listing.goalAmount -= web3.utils.fromWei(x.data.result.value);
listing.save();
res.send(listing);
}
});
}
});
});
app.delete("/crowdfund/:userAddress", ensureUserOwnsListing, (req, res) => {
const userAddress = req.body.userAddress;
Listing.findOne({ userAddress: userAddress }, (err, listing) => {
if (err) {
console.log(err);
} else {
listing.deleteOne({ userAddress: userAddress });
}
});
});
app.post("/crowdfunds", (req, res) => {
Listing.find({}, (err, result) => {
if (err) {
console.log(err);
} else {
res.json(result);
}
});
});
app.listen(port);
console.log(`Server started on port ${port}!`);