-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (48 loc) · 1.56 KB
/
app.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
const { ApolloServer } = require('apollo-server-express');
const express = require('express');
const typeDefs = require('./typeDefs');
const resolvers = require('./resolvers');
const config = require('./config');
const utils = require('./utils');
const morgan = require('morgan');
const mongoose = require('mongoose');
var cors = require('cors')
var whitelist = ['http://127.0.0.1:4200', 'http://127.0.0.1:4000'];
var corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
},
optionsSuccessStatus: 200
}
const startServer = async () => {
const app = express();
app.use(cors());
app.use(morgan("tiny"));
app.use(function (_, __, next) {
console.log('\x1b[33m%s\x1b[0m', "--------------------------");
next();
});
app.use((req, res, next) => {
//console.log(req.headers);
next();
});
app.use('/download', require('./download.js').router);
await mongoose.connect(config.database, { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.set('useFindAndModify', false);
const server = new ApolloServer({
typeDefs, resolvers, context: ({ req, res }) => {
const token = req.headers.authorization || '';
const { payload, loggedIn } = utils.getPayload(token);
return { payload, loggedIn };
}
});
server.applyMiddleware({ app });
app.listen({ port: config.PORT }, () => {
console.log(`API runs at http://localhost:${config.PORT}${server.graphqlPath}`)
})
}
startServer();