-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
70 lines (63 loc) · 1.57 KB
/
server.ts
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
import 'dotenv-safe/config';
import { ApolloServer } from 'apollo-server-express';
import express from 'express';
import mongoose from 'mongoose';
import cookieParser from 'cookie-parser';
import cookieSession from 'cookie-session';
import cors from 'cors';
import colors from 'colors';
import schema from './server/graphql/schema';
const PORT = process.env.PORT || 5000;
const startServer = async () => {
const app = express();
app.use(
cors({
credentials: true,
origin:
process.env.NODE_ENV === 'production'
? 'prod url'
: 'http://localhost:3000'
})
);
app.use(cookieParser());
app.use(
cookieSession({
name: 'session',
keys: ['secret key'],
httpOnly: true,
maxAge: 4 * 60 * 60 * 1000 // 4 hours
})
);
mongoose
.connect(process.env.MONGO_URI as string, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
autoIndex: process.env.NODE_ENV !== 'production'
})
.catch(err =>
console.log(colors.red(`Error connecting to mongo: ${err.message}`))
);
const server = new ApolloServer({
schema,
playground: true,
context: ({ req, res }) => ({
req,
res
})
});
server.applyMiddleware({
app,
cors: false,
path: '/graphql'
});
app.listen({ port: PORT }, (): void => {
console.log(
`\n🚀 GraphQL is now running on http://localhost:${PORT}/graphql`
);
});
};
startServer().catch(err =>
console.error(colors.red(`Error starting server ${err.message}`))
);