-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (56 loc) · 1.98 KB
/
index.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
const jwt = require("jsonwebtoken");
const mongoose = require("mongoose");
const { ApolloServer } = require("apollo-server-express");
const express = require("express");
const {graphqlUploadExpress} = require("graphql-upload");
const typeDefs = require("./gql/schema");
const resolvers = require("./gql/resolver");
require("dotenv").config({ path: ".env"});
mongoose.connect(process.env.BBDD,{
useNewUrlParser: true,
useUnifiedTopology: true
}, (err, _)=> {
if(err){
console.log("Error de conexion");
console.log(err);
}else{
//console.log(process.env.BBDD);
server();
}
});
async function server() {
const serverApollo = new ApolloServer({
typeDefs,
resolvers,
context: ({req}) => {
//console.log(req.headers.authorization);
const token = req.headers.authorization;
console.log("Header recibido es: " + token);
if (token){
try {
const user = jwt.verify(
token.replace("Bearer ",""),
process.env.SECRET_KEY
);
return{
user,
}
} catch (error) {
console.log("### ERROR ###");
console.log(error);
throw new Error("Token Inválido");
}
}
}
});
await serverApollo.start();
const app = express();
app.use(graphqlUploadExpress());
serverApollo.applyMiddleware({app});
await new Promise((r) => app.listen({port: process.env.PORT || 4000},r));
// serverApollo.listen().then(({url})=>{
console.log("###################################");
console.log(`Servidor listo en la url http://localhost:4000${serverApollo.graphqlPath}`);
console.log("###################################");
// });
}