-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (71 loc) · 2.31 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const path = require('path');
const express = require('express');
const cors = require('cors');
const graphqlExpress = require('express-graphql');
const mongoose = require('mongoose');
const { makeExecutableSchema } = require('graphql-tools');
const glue = require('schemaglue');
const app = express();
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const passport = require('passport');
const users = require('./routes/users');
// view engine setup
const GRAPHQL_PORT = process.env.PORT || 5000;
//const WS_PORT = 4090;
const MONGO_URL = process.env.MONGO_URL || 'mongodb://localhost:27017/graphql';
let jwt = require('express-jwt');
let auth = jwt({ secret: process.env.TODO_BACKEND_SECRET });
require('./config/passport');
app.use(cors());
app.use(cookieParser());
app.use(passport.initialize());
const { schema, resolver } = glue('./graphql', {});
const executableSchema = makeExecutableSchema({
typeDefs: schema,
resolvers: resolver
});
//console.log('test');
app.use(
'/graphql',
graphqlExpress(req => ({
schema: executableSchema,
graphiql: true,
logger: { log: e => console.log(e) }
}))
);
app.listen(GRAPHQL_PORT, () =>
console.log(
`GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}/graphql (ノ◕ヮ◕)ノ*:・゚✧`
)
);
/*
app.head('/graphql', (req, res) => {
res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
res.header('Access-Control-Request-Method', 'GET, POST');
res.header('Access-Control-Allow-Headers', 'Origin, Accept, Content-Type, Content-Length');
res.end();
});*/
mongoose.connect(
MONGO_URL,
{ useNewUrlParser: true }
);
mongoose.connection
// eslint-disable-next-line
.on('error', function(err) {
console.log(
'Error: Could not connect to MongoDB. Did you forget to run `mongod`?'.red
);
})
.on('open', function() {
console.log('Connection extablised with MongoDB');
});
//app.use('/', indexRouter);
//app.use(bodyParser.urlencoded({ extended: false }))
app.use('/users', users);
app.use(express.static(__dirname + '/dist/todoApp'));
app.all('*', (req, res) => {
const indexFile = `${path.join(__dirname, 'dist')}/todoApp/index.html`;
res.status(200).sendFile(indexFile);
});
module.exports = app;