【问题标题】:Adding https to a graphql express endpoint proxied by webpack将 https 添加到 webpack 代理的 graphql express 端点
【发布时间】: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


    【解决方案1】:

    我认为您希望保护任何面向公众的端点。不推荐在生产环境中使用 webpack 开发服务器,但如果您确实需要使用 https,那么 webpack 开发服务器确实提供了support for doing so

    【讨论】:

    • 您知道该选项是保护代理还是仅保护输出?
    • 它只会将 webpack 开发服务器放在 https 后面,而不是 express 服务器。为了确保 express 的安全,您还需要查看有关如何做到这一点的文档。
    • 我一直在尝试从文档中复制示例。这里奇怪的是 express-graphql 将 express 服务器作为参数,然后 webpack 代理 express-graphql 服务器。所以有 3 种不同的 API,我不知道哪些需要 ssl 配置。
    猜你喜欢
    • 2021-05-04
    • 2011-10-01
    • 1970-01-01
    • 2018-03-05
    • 2016-06-11
    • 2020-03-07
    • 2015-02-09
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多