从浏览器连接到 apollo graphQL 服务器时出现 404 错误
Get 404 error when connecting to apollo graphQL server from browser
我在 nodejs 中设置了 apollo graphql 服务器。下面是源代码。我可以启动服务器,它开始侦听端口 6000。但是当我在浏览器中打开 url (http://localhost:6000/graphiql
) 时,我得到了 This site can’t be reached
。我想知道我的代码有什么问题。
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!');
});
我在 nodejs 中设置了 apollo graphql 服务器。下面是源代码。我可以启动服务器,它开始侦听端口 6000。但是当我在浏览器中打开 url (http://localhost:6000/graphiql
) 时,我得到了 This site can’t be reached
。我想知道我的代码有什么问题。
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!');
});