-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
97 lines (84 loc) · 2.56 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
94
95
96
97
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const express = require("express");
const { ApolloServer, PubSub } = require("apollo-server-express");
const typeDefs = require("./graphql/typeDefs");
const resolvers = require("./graphql/resolvers");
const pino = require("express-pino-logger")();
const cors = require("cors");
const app = express();
// let origin = "http://localhost:3000";
// if (process.env.NODE_ENV === "production") {
// origin = /vercel\.app$/;
// } else {
// origin = "http://localhost:3000";
// }
// app.use(
// cors({
// origin: origin,
// credentials: true,
// })
// );
// app.use(express.json());
app.use(pino);
process.on("uncaughtException", (err) => {
console.error("Uncaught Exception");
console.error(err);
console.error("Shutting Down the server...");
// kill
process.exit(1);
});
dotenv.config();
const pubsub = new PubSub();
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => ({ req, pubsub }),
});
server.applyMiddleware({ app, path: "/" });
let DB = "";
if (process.env.NODE_ENV === "production") {
DB = process.env.DATABASE.replace("<PASSWORD>", process.env.DB_PASSWORD);
} else {
DB = process.env.LOCAL_DB;
}
const PORT = process.env.PORT || 4000;
// app.all("/*", (req, res, next) => {
// // const err = new Error(`${req.url} does not exist`);
// // err.statusCode = 404;
// // err.status = 'fail';
// res.status(404).json({
// status: "fail",
// message: `${req.url} does not exist. Use /graphql to access the graphql api`,
// });
// });
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
.then((con) => {
console.log("Database Connected");
app.listen({ port: PORT }, () => {
console.log(
`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`
);
});
})
.catch((err) => console.error("Database Connection Failure"));
// app.listen({ port: 5000 }, () =>
// console.log(`🚀 Server ready at http://localhost:5000${server.graphqlPath}`)
// );
process.on("unhandledRejection", (err) => {
console.log("Unhandled Rejection");
console.error(err);
console.log("Shutting Down the server...");
// safely close
db.close();
server.close(() => {
// kill
process.exit(1);
});
});