-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
69 lines (58 loc) · 1.89 KB
/
app.js
File metadata and controls
69 lines (58 loc) · 1.89 KB
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
const express = require('express');
const app = express();
const morgan = require('morgan');
const cors = require('cors');
const bodyParser = require('body-parser');
const routes = require('./api/routes/index');
const graphQlHttp = require('express-graphql');
require('dotenv').config();
const events = [];
const mongoose = require('mongoose');
const graphqlResolvers = require('./api/graphql/resolvers/index');
const graphqlSchemas = require('./api/graphql/schemas/index');
const isAuth = require('./api/graphql/midlleware/is-auth');
mongoose.connect("mongodb+srv://" + process.env.MONGO_ATLAS_USER + ":" + process.env.MONGO_ATLAS + "@restapi-kvyex.mongodb.net/" + process.env.MONGO_ATLAS_DATABASE + "?retryWrites=true",
{useNewUrlParser: true}, function (err) {
if (!err) {
console.log('Successful Connection to MongoDB');
} else {
console.log('Error Loading Connection', err.message);
}
});
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(morgan('dev'));
app.use(cors());
app.use(isAuth);
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST,GET,OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type', 'Authorization');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
})
app.use('/', routes);
app.use('/graphql', graphQlHttp({
schema: graphqlSchemas,
rootValue: graphqlResolvers,
graphiql: true
}));
app.use('/favicon.ico', () => {
console.log("Error")
});
app.use(function (req, res, next) {
const error = new Error('Route Not Found');
error.status = 404;
next(error);
});
app.use(function (err, req, res) {
res.status(err.status || 500);
res.json({
err: {
message: err.message
}
})
});
module.exports = app;