-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
102 lines (88 loc) · 2.37 KB
/
Copy pathserver.js
File metadata and controls
102 lines (88 loc) · 2.37 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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
require("dotenv").config({ path: "variables.env" });
const Recipe = require("./models/Recipe");
const User = require("./models/User");
const cors = require("cors");
const jwt = require("jsonwebtoken");
const path = require("path");
// Bring in GraphQL - Express middleware
const { graphiqlExpress, graphqlExpress } = require("apollo-server-express");
const { makeExecutableSchema } = require("graphql-tools");
const { typeDefs } = require("./schema");
const { resolvers } = require("./resolvers");
// Create schema
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
// Connect to Database
// console.log(process.env.MONGO_URI);
mongoose
.connect(process.env.MONGO_URI)
.then(() => {
console.log("DB connected");
})
.catch(err => {
console.log(err);
});
// Initial Express Server
const app = express();
//Enable CORS for server
const corsOptions = {
origin: "http://localhost:3000",
credentials: true
};
app.use(cors(corsOptions));
// Setup JWT authentication middleware
app.use(async (req, res, next) => {
const token = req.headers["authorization"];
// console.log(token, typeof token);
if (token !== "null") {
try {
const currentUser = await jwt.verify(token, process.env.SECRET);
// console.log(currentUser);
// Assign current user in to request
req.currentUser = currentUser;
} catch (error) {
console.log(error);
}
}
next();
});
// Create GraphQL application
app.use("/graphiql", graphiqlExpress({ endpointURL: "/graphql" }));
// Connect schema with GraphQL
app.use(
"/graphql",
bodyParser.json(),
// User options function to get (req, res, next) of route
graphqlExpress(({ currentUser }) => ({
schema,
context: {
// Add assigned user above in to context
currentUser,
Recipe,
User
}
}))
// graphqlExpress({
// schema,
// // pass MongoDB model into context
// context: {
// Recipe,
// User
// }
// })
);
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 || 4444;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});