-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
93 lines (76 loc) · 2.09 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
import dotenv from "dotenv"; // pull env variables from .env file
import { ApolloServer } from "apollo-server-express";
import { graphqlUploadExpress, GraphQLUpload } from "graphql-upload";
import express from "express";
import path from "path";
import http from "http";
import cors from "cors";
import rateLimit from "express-rate-limit";
import xss from "xss-clean";
import mongoSanitize from "express-mongo-sanitize";
import chalk from "chalk";
import { importSchema } from "graphql-import";
import Query from "./resolvers/Query.js";
import Mutation from "./resolvers/Mutation.js";
import Contact from "./resolvers/Contact.js";
import isAuth from "./middlewares/isAuth.js";
import database from "./db.js";
//start
dotenv.config();
const typeDefs = importSchema("./schema/schema.graphql");
const apiLimiter = rateLimit({
windowMs: 60 * 60 * 1000,
max: 50000,
message: "Too many requests from this IP, please try again after 15 minutes",
});
const resolvers = {
Query,
Mutation,
Contact,
};
const PORT = process.env.PORT;
const app = express();
app.use(apiLimiter);
app.use(xss());
app.use(mongoSanitize());
app.use(cors());
app.use(graphqlUploadExpress());
// app.use("/uploads", express.static(path.join(, "./uploads")));
app.get("/", (req, res) =>
res.json({ "Contact Book version": "v1", status: "healthy" })
);
const httpServer = http.createServer(app);
const server = new ApolloServer({
typeDefs,
uploads: false,
resolvers: {
...resolvers,
Upload: GraphQLUpload,
},
playground: true,
introspection: true,
context: ({ req, res, connection }) => {
if (connection) {
return { ...connection, res, req };
} else {
return { ...isAuth(req), res, req };
}
},
});
server.applyMiddleware({ app });
database
.connect()
.then(() => {
httpServer.listen(PORT || 4000, () => {
console.log(
chalk
.hex("#fab95b")
.bold(
`🚀 Server ready at http://localhost:${process.env.PORT || 4000}${
server.graphqlPath
}`
)
);
});
})
.catch((e) => console.log(chalk.red(e)));