【发布时间】:2016-05-06 13:04:40
【问题描述】:
我正在使用 webpack、express 和 graphql 构建一个应用程序。 Express 服务于 express-graphql 端点,然后由 webpack-dev-server 代理。我想知道如何将 https 添加到该端点。
我没有使用 ssl 的经验,并且使用 express 和 webpack 的经验有限。我什至不确定问题是如何保护代理、快速服务器或 express-graphql 端点。
附带说明一下,我还使用 auth0 添加了用户身份验证,这似乎工作正常。
server.js
import express from 'express';
import graphQLHTTP from 'express-graphql';
import jwt from 'express-jwt';
import path from 'path';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import {Schema} from './data/schema';
const APP_PORT = process.env.PORT || 3000;
const GRAPHQL_PORT = 8080;
const AUTH0_ID = process.env.AUTH0_ID;
const AUTH0_SECRET = process.env.AUTH0_SECRET;
const authenticate = jwt({
secret: new Buffer(AUTH0_SECRET, 'base64'),
audience: AUTH0_ID,
});
// Expose a GraphQL endpoint
const graphQLServer = express();
graphQLServer.use('/', authenticate, graphQLHTTP(request => ({
graphiql: true,
pretty: true,
schema: Schema,
rootValue: { user: request.user },
})));
graphQLServer.listen(GRAPHQL_PORT, () => console.log(
`GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}`
));
// Serve the Relay app
...
const app = new WebpackDevServer(compiler, {
contentBase: '/public/',
proxy: {'/graphql': `http://localhost:${GRAPHQL_PORT}`},
publicPath: '/app/',
stats: {colors: true, chunks: false},
});
// Handle incoming routes
app.use('/', (req, res) => {
res.sendFile(path.join(__dirname + '/public/index.html'));
});
app.listen(APP_PORT, () => {
console.log(`App is now running on http://localhost:${APP_PORT}`);
});
【问题讨论】:
标签: javascript express https webpack graphql