Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
116 lines (108 loc) · 3.17 KB
/
Copy pathApp.js
File metadata and controls
116 lines (108 loc) · 3.17 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
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const graphqlHttp = require("express-graphql");
const graphqlSchema = require("./Graphql/Schemas/Mainschema");
const graphqlResolvers = require("./Graphql/Resolvers/Mainresolver");
const path = require("path");
const cookieparser = require("cookie-parser");
const cors = require("cors");
const cards = require("./Models/Cards");
const upload = require("./Multer/Multer");
const jwt = require("jsonwebtoken");
const { user, facebookuser, gmailuser } = require("./Models/Users");
const app = express();
app.use(bodyParser.json());
app.use(cookieparser());
app.use(bodyParser.urlencoded({ extended: true }));
console.log(process.env.ORIGIN);
app.use(
cors({
credentials: true,
origin: process.env.ORIGIN,
})
);
app.use("/images", express.static(path.join(__dirname, "images")));
app.use((req, res, next) => {
res.setHeader("Aceess-Control-Allow-Credentials", true),
res.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
if (req.method === "OPTIONS") {
return res.sendStatus(200);
}
next();
});
app.get("/test", async (req, res, next) => {
res.send("whats up");
});
app.get("/checklogin", async (req, res, next) => {
jwt.verify(
req.cookies.token,
process.env.APP_SECRET,
async (err, decoded) => {
if (err) {
res.send("no user");
}
let founduser;
if (decoded.usertype === "archos") {
founduser = await user.findById(decoded.id);
res.send({
usertype: "archos",
name: founduser.name,
username: founduser.username,
lastname: founduser.lastname,
});
} else if (decoded.usertype === "facebook") {
founduser = await facebookuser.findById(decoded.id);
res.send({
usertype: "facebook",
name: founduser.name,
username: founduser.username,
lastname: founduser.lastname,
});
} else {
founduser = await gmailuser.findById(decoded.id);
res.send({
usertype: "gmail",
name: founduser.name,
username: founduser.username,
lastname: founduser.lastname,
});
}
}
);
});
app.post("/uploadimage", upload.single("image"), async (req, res, next) => {
try {
await cards
.findById(req.body._id)
.exec()
.then((result) => {
result.set("image", req.file.location);
result.save();
});
} catch (err) {
throw new Error("Server error");
}
res.send(true);
});
const graphqlfunc = graphqlHttp((req, res) => {
return {
schema: graphqlSchema,
rootValue: graphqlResolvers,
context: { req, res },
graphiql: true,
};
});
app.use("/graphql", async (req, res, next) => {
try {
await graphqlfunc(req, res);
} catch (err) {}
});
mongoose
.connect(
"mongodb+srv://Archos123:jldgnWgqRelkGcCl@archos-dqxu9.mongodb.net/Main?retryWrites=true&w=majority",
{ useNewUrlParser: true, useUnifiedTopology: true }
)
.then(() => app.listen(process.env.PORT || 4000));