-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (58 loc) · 2.12 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
require("dotenv").config();
const express = require("express");
const models = require("./server/models");
const expressGraphQL = require("express-graphql");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const passportConfig = require("./server/services/auth");
const MongoStore = require("connect-mongo");
const schema = require("./server/schema/schema");
// Create a new Express application
const app = express();
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// Replace with your mongoLab URI
const MONGO_URI = process.env.MONGO_URI;
// Mongoose's built in promise library is deprecated, replace it with ES2015 Promise
mongoose.Promise = global.Promise;
// Connect to the mongoDB instance and log a message
// on success or failure
mongoose.connect(MONGO_URI);
mongoose.connection
.once("open", () => console.log("Connected to MongoLab instance."))
.on("error", error => console.log("Error connecting to MongoLab:", error));
// Configures express to use sessions. This places an encrypted identifier
// on the users cookie. When a user makes a request, this middleware examines
// the cookie and modifies the request object to indicate which user made the request
// The cookie itself only contains the id of a session; more data about the session
// is stored inside of MongoDB.
app.use(
session({
resave: true,
saveUninitialized: true,
secret: process.env.SECRET,
store: MongoStore.create({
mongoUrl: MONGO_URI,
autoReconnect: true
})
})
);
// Passport is wired into express as a middleware. When a request comes in,
// Passport will examine the request's session (as set by the above config) and
// assign the current user to the 'req.user' object. See also servces/auth.js
app.use(passport.initialize());
app.use(passport.session());
// Instruct Express to pass on any request made to the '/graphql' route
// to the GraphQL instance.
app.use(
"/graphql",
expressGraphQL({
schema,
graphiql: true
})
);
app.listen(process.env.PORT || 3001, () => {
console.log("Listening");
});