Created
August 31, 2019 15:50
-
-
Save DerPauli/c2768c55d8f63db5970bca1dfef99ed9 to your computer and use it in GitHub Desktop.
Apollo https requirement -> generate self signed certificate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from 'express' | |
import { ApolloServer } from 'apollo-server-express' | |
import typeDefs from './graphql/schema' | |
import resolvers from './graphql/resolvers' | |
import fs from 'fs' | |
import https from 'https' | |
import http from 'http' | |
const configurations = { | |
// Note: You may need sudo to run on port 443 | |
production: { ssl: true, port: 443, hostname: 'example.com' }, | |
development: { ssl: false, port: 4000, hostname: 'localhost' } | |
} | |
const environment = process.env.NODE_ENV || 'production' | |
const config = configurations[environment] | |
const apollo = new ApolloServer({ typeDefs, resolvers }) | |
const app = express() | |
apollo.applyMiddleware({ app }) | |
// Create the HTTPS or HTTP server, per configuration | |
var server | |
if (config.ssl) { | |
// Assumes certificates are in .ssl folder from package root. Make sure the files | |
// are secured. | |
server = https.createServer( | |
{ | |
key: fs.readFileSync(`./ssl/${environment}/server.key`), | |
cert: fs.readFileSync(`./ssl/${environment}/server.crt`) | |
}, | |
app | |
) | |
} else { | |
server = http.createServer(app) | |
} | |
// Add subscription support | |
apollo.installSubscriptionHandlers(server) | |
server.listen({ port: config.port }, () => | |
console.log( | |
'🚀 Server ready at', | |
`http${config.ssl ? 's' : ''}://${config.hostname}:${config.port}${apollo.graphqlPath}` | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment