-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
46 lines (38 loc) · 1.15 KB
/
Copy pathserver.js
File metadata and controls
46 lines (38 loc) · 1.15 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
require('dotenv').config();
const express = require('express');
const routes = require('./routes');
const cors = require('cors');
const app = express();
const mongoose = require('mongoose');
const graphqlHTTP = require('express-graphql');
const graphqlSchema= require('./graphql/Schema.js');
mongoose.Promise = global.Promise;
mongoose.connect(process.env.DATABASE, { useMongoClient: true,}, (err) => {
if(err) {
console.log(err);
}
});
//Or with promise
// mongoose.connect(process.env.DATABASE, { useMongoClient: true,}).then(
// err => {
// console.log(err);
// }
// );
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
/**** Middleware ****/
// Cross origin resource sharing (CORS)
app.use(cors());
// Serves static files from public folder
app.use(express.static('public'));
/**** Routes ****/
app.use('/graphql', graphqlHTTP ({
schema: graphqlSchema,
graphiql:true
}));
app.use('/', routes);
// start the server
const listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});
module.exports = app; // used for testing