-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (55 loc) · 1.72 KB
/
app.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
const express = require("express");
const expressPlayground = require("graphql-playground-middleware-express")
.default;
const graphqlHTTP = require("express-graphql");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const graphQlSchema = require("./graphql/typeDefs");
const graphQlResolvers = require("./graphql/resolvers");
const isAuth = require("./middleware/is-auth");
const app = express();
app.use(bodyParser.json());
// Custom CORS
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
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.use(isAuth);
// Connect Graphql
app.use(
"/graphql",
graphqlHTTP({
schema: graphQlSchema,
rootValue: graphQlResolvers,
graphiql: true,
})
);
app.get("/playground", expressPlayground({ endpoint: "/graphql" }));
app.use("/", (req, res) =>
res.send(
"Welcome to User blog GraphQL API, use /playground or /graphql for playground"
)
);
// Initialize app
const startApp = async () => {
try {
// Database Connection
await mongoose.connect(
`mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@user-profile-9xaur.mongodb.net/${process.env.DB_NAME}?retryWrites=true`,
{ useNewUrlParser: true, useUnifiedTopology: true }
);
console.log(`*** Connected to database ***`);
const PORT = process.env.PORT || 5000;
app.listen({ port: PORT }, () =>
console.log(`*** Server running on: http://localhost:${PORT} ***`)
);
} catch (err) {
console.log(err.message);
}
};
startApp();