从浏览器连接到apollo graphQL服务器时出现404错误

从浏览器连接到apollo graphQL服务器时出现404错误,graphql,apollo-server,Graphql,Apollo Server,我在nodejs中设置了apollo graphql服务器。下面是源代码。我可以启动服务器,它开始监听端口6000。但是当我打开url(http://localhost:6000/graphiql)在浏览器中。我想知道我的代码出了什么问题 const express = require('express'); const bodyParser = require('body-parser'); const { graphqlExpress, graphiqlExpress } = require

我在nodejs中设置了apollo graphql服务器。下面是源代码。我可以启动服务器,它开始监听端口6000。但是当我打开url(
http://localhost:6000/graphiql
)在浏览器中。我想知道我的代码出了什么问题

const express = require('express');
const bodyParser = require('body-parser');
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express');
const { makeExecutableSchema } = require('graphql-tools');

// Some fake data
const books = [
  {
    title: "Harry Potter and the Sorcerer's stone",
    author: 'J.K. Rowling',
  },
  {
    title: 'Jurassic Park',
    author: 'Michael Crichton',
  },
];

// The GraphQL schema in string form
const typeDefs = `
  type Query { books: [Book] }
  type Book { title: String, author: String }
`;

// The resolvers
const resolvers = {
  Query: { books: () => books },
};

// Put together a schema
const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
});

// Initialize the app
const app = express();

// The GraphQL endpoint
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));

// GraphiQL, a visual editor for queries
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));

// Start the server
app.listen(6000, () => {
  console.log('Go to http://localhost:6000/graphiql to run queries!');
});

这是一个端口问题,端口<代码> 6000 /代码>是大多数浏览器不考虑安全的端口之一,即使您可以在终端上卷曲。例如,这应该起作用:

curl -X POST http://localhost:6000/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ books { title } }"}'
但不应该在浏览器上运行它,因为端口受到限制。 您可以在和上看到受限端口列表,其他浏览器也应遵循此规则


如果您将端口更改为4000(例如,或任何其他不受限制的端口),一切都应该正常工作。

< P>这是一个端口问题,端口<代码> 6000 > /代码>是大多数浏览器不考虑安全的端口之一,即使您可以在终端上卷曲。例如,这应该起作用:

curl -X POST http://localhost:6000/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ books { title } }"}'
但不应该在浏览器上运行它,因为端口受到限制。 您可以在和上看到受限端口列表,其他浏览器也应遵循此规则

如果您将端口更改为4000(例如,或任何其他不受限制的端口),则一切正常