Skip to content

Commit dd11382

Browse files
committed
Preparando API para production
1 parent 24719c7 commit dd11382

4 files changed

Lines changed: 34 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,4 @@ query getPeopleDate($monitor: Boolean!, $avatar: Boolean!) {
304304
}
305305
```
306306

307-
> *Nota se crearon los indexes -> `db.courses.createIndex({ "$**": "text" })`*
307+
> *Nota se crearon los indexes -> `db.courses.createIndex({ "$**": "text" }); db.students.createIndex({ "$**": "text" });`*

index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
require('dotenv').config()
44
const { makeExecutableSchema } = require('graphql-tools') // Nota: igual se necesita la dependencia de graphql porque graphql-tools la necesita.
55
const express = require('express')
6+
const cors = require('cors')
67
const gqlMiddleware = require('express-graphql')
78
const { readFileSync } = require('fs')
89
const { join } = require('path')
910
const resolvers = require('./lib/resolvers')
1011

1112
const app = express()
1213
const port = process.env.port || 3000
14+
const isDev = process.env.NODE_ENV !== 'production'
1315

1416
// Definiendo el esquema
1517
const typeDefs = readFileSync(
@@ -18,10 +20,23 @@ const typeDefs = readFileSync(
1820
)
1921
const schema = makeExecutableSchema({ typeDefs, resolvers })
2022

23+
const whitelist = ['http://localhost']
24+
const corsOptionsDelegate = function (req, callback) {
25+
var corsOptions;
26+
if (whitelist.indexOf(req.header('Origin')) !== -1) {
27+
corsOptions = { origin: true } // reflect (enable) the requested origin in the CORS response
28+
} else {
29+
corsOptions = { origin: false } // disable CORS for this request
30+
}
31+
callback(null, corsOptions) // callback expects two parameters: error and options
32+
}
33+
34+
app.use(cors(corsOptionsDelegate))
35+
2136
app.use('/api', gqlMiddleware({
2237
schema: schema,
2338
rootValue: resolvers,
24-
graphiql: true
39+
graphiql: isDev
2540
}))
2641

2742
app.listen(port, () => {

package-lock.json

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7+
"start": "NODE_ENV=production node index",
78
"dev": "nodemon -e js,graphql index",
89
"lint": "standard",
910
"lint-fix": "standard --fix"
1011
},
11-
"keywords": ["node.js", "graphql", "express"],
12+
"keywords": [
13+
"node.js",
14+
"graphql",
15+
"express"
16+
],
1217
"author": "John Serrano <jandreys15@gmail.com> (https://twitter.com/Jandrey15)",
1318
"license": "ISC",
1419
"dependencies": {
20+
"cors": "2.8.5",
1521
"dotenv": "8.0.0",
1622
"express": "4.17.1",
1723
"express-graphql": "0.9.0",

0 commit comments

Comments
 (0)