graphql - 从浏览器连接到 apollo graphQL 服务器时出现 404 错误
问题描述
我在 nodejs 中设置了 apollo graphql 服务器。下面是源代码。我可以启动服务器并开始侦听端口 6000。但是当我在浏览器中This site can’t be reached
打开 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 } }"}'
但是不应该在浏览器上运行它,因为端口受到限制。您可以在Chrome和Firefox上查看受限端口列表,其他浏览器也应遵循此规则。
如果您将端口更改为 4000(例如,或任何其他不受限制的端口),一切都应该可以正常工作。
推荐阅读
- r - X 必须是一个数值
- node.js - router.get 发送 res 两次
- php - 在 laravel 控制器中访问中间件
- php - 如何使用 str_split() 函数逐字符打印字符串
- google-cloud-platform - 连接被拒绝:在将 Google Cloud postgresql 与 Cloud Function 连接时
- phpspreadsheet - PhpSpreadsheet 注意:未定义的偏移量:4
- python - 如何在元组列表中将字符串转换为 Int?
- c++ - 组织模拟结果
- elasticsearch - 有没有一种方法可以解析特定字段以在 kibana 中进行可视化
- angular - 我正在尝试计算每个行项目的总数,但在更改数量时它只计算数组中的第一个总数