Zha*_* Yi 2 graphql apollo-server
我在nodejs 中设置了apollo graphql 服务器。下面是源代码。我可以启动服务器,它开始侦听端口 6000。但是This site can\xe2\x80\x99t be reached当我在浏览器中打开 url ( http://localhost:6000/graphiql) 时,我得到了。我想知道我的代码有什么问题。
const express = require(\'express\');\nconst bodyParser = require(\'body-parser\');\nconst { graphqlExpress, graphiqlExpress } = require(\'apollo-server-express\');\nconst { makeExecutableSchema } = require(\'graphql-tools\');\n\n// Some fake data\nconst books = [\n {\n title: "Harry Potter and the Sorcerer\'s stone",\n author: \'J.K. Rowling\',\n },\n {\n title: \'Jurassic Park\',\n author: \'Michael Crichton\',\n },\n];\n\n// The GraphQL schema in string form\nconst typeDefs = `\n type Query { books: [Book] }\n type Book { title: String, author: String }\n`;\n\n// The resolvers\nconst resolvers = {\n Query: { books: () => books },\n};\n\n// Put together a schema\nconst schema = makeExecutableSchema({\n typeDefs,\n resolvers,\n});\n\n// Initialize the app\nconst app = express();\n\n// The GraphQL endpoint\napp.use(\'/graphql\', bodyParser.json(), graphqlExpress({ schema }));\n\n// GraphiQL, a visual editor for queries\napp.use(\'/graphiql\', graphiqlExpress({ endpointURL: \'/graphql\' }));\n\n// Start the server\napp.listen(6000, () => {\n console.log(\'Go to http://localhost:6000/graphiql to run queries!\');\n});\nRun Code Online (Sandbox Code Playgroud)\n
这是一个端口问题,端口6000是大多数浏览器认为不安全的端口之一,即使您可以在终端上卷曲它。例如这应该有效:
curl -X POST http://localhost:6000/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ books { title } }"}'
Run Code Online (Sandbox Code Playgroud)
但在浏览器上运行它不应该,因为端口受到限制。您可以在Chrome和Firefox上看到受限端口列表,其他浏览器也应遵循此规则。
如果您将端口更改为 4000(例如,或任何其他不受限制的端口),一切都会正常工作。
| 归档时间: |
|
| 查看次数: |
5517 次 |
| 最近记录: |