-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (61 loc) · 1.86 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
/**
* Main application file
*/
const morgan = require('morgan');
const express = require('express');
const http = require('http');
const { ApolloServer, gql } = require('apollo-server');
const Product = require('./api/product/product.model');
const mongoose = require('mongoose');
const expressConfig = require('./config/express');
const routeConfig = require('./routes');
const config = require('./config/environment');
const schema = require('./graphql');
// Graphql necesita un esquema para poder lanzar un servidor. Entonces le pasamos typedefs
// Y deberia tener un resolver tambien.
// Aqui tambien se puede especificar un formato de error con:
// formatError: (err)
const graphqlServer = new ApolloServer({
schema,
dataSources: () => {
Product,
},
});
// Connect to MongoDB
mongoose.connect(config.mongo.uri, { useNewUrlParser: true, useFindAndModify: false });
mongoose.connection.on('error', (err) => {
console.error('Error', 'MongoDB connection error', {
data: err,
time: new Date().toISOString(),
});
process.exit(-1);
});
// Setup server
const app = express();
const server = http.createServer(app);
app.use(morgan('dev'));
expressConfig(app);
routeConfig(app);
// Start server
function startServer() {
app.shoppingCartBK = server.listen(config.port, config.ip, () => {
console.log(`Express server listening on ${config.port}, in ${app.get('env')} mode`);
});
// Iniciar el servidor de graphql por defecto en el puerto 4000
// En esta url graphql nos da un playground.
// Aunque postman ya lo soporta
graphqlServer.listen().then(( { url }) => console.log(`Server running at ${url}`));
// Ya en el playground podemos dar (query o mutacion):
// query {
// hello
// }
// Y responde:
// {
// "data": {
// "hello": "Hola MedellinJS"
// }
// }
}
setImmediate(startServer);
// Expose app
module.exports = app;