-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
82 lines (70 loc) · 1.78 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
require("dotenv").config();
const express = require("express");
const { ApolloServer } = require("apollo-server-express");
const helmet = require("helmet");
const typeDefs = require("./graphQL/schema");
const resolvers = require("./graphQL/resolvers");
const Traders = require("./models/model");
const Users = require("./models/databankUsers");
const cors = require("cors");
const morgan = require("morgan");
const server = new ApolloServer({
helmet,
typeDefs,
resolvers,
context() {
return { Traders, Users };
},
introspection: true,
playground: true
});
const app = express();
const corsOptions = {
// switch to https://sauti.now.sh/ when ready for lance
origin: "*",
methods: ["GET", "OPTIONS", "POST"],
allowedHeaders: [
"Content-Type",
"Authorization",
"Content-Length",
"X-Requested-With",
"Accept"
]
};
app.get("/", function ping(req, res) {
res.status(200).json({ api: "Running." });
});
// app.use(morgan("dev"))
app.use(express.json());
app.use(cors(corsOptions));
server.applyMiddleware({
app,
path: "/graphql",
cors: false // disabling the apollo-server-express cors to allow the cors middleware use
});
app.use(function notFound(req, res, next) {
const error = new Error("Not found");
error.status = 404;
next(error);
});
app.use(function catchUnhandledPromiseRejections(error, req, res, next) {
if (error instanceof Promise) {
error.catch(err => {
next(error);
});
} else {
next(error);
}
});
app.use(function catchAll(error, req, res, next) {
error.status = error.status || 500;
error.message = error.message || "Internal server error";
console.error(error);
res.status(error.status).json({
error: {
message: error.message,
status: error.status
}
});
});
module.exports = app;