【发布时间】:2018-01-19 09:21:53
【问题描述】:
我是 nodeJS 的新手,我正在尝试关注这个tutorial。
我的代码:
// server/index.js
import express from 'express';
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express';
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
import bodyParser from 'body-parser';
import { createServer } from 'http';
import { Schema } from './data/schema';
import { Mocks } from './data/mocks';
const GRAPHQL_PORT = 8000;
const app = express();
const executableSchema = makeExecutableSchema({
typeDefs: Schema,
});
addMockFunctionsToSchema({
schema: executableSchema,
mocks: Mocks,
preserveResolvers: true,
});
// `context` must be an object and can't be undefined when using connectors
app.use('/graphql', bodyParser.json(), graphqlExpress({
schema: executableSchema,
context: {}, // at least(!) an empty object
}));
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
const graphQLServer = createServer(app);
graphQLServer.listen(GRAPHQL_PORT, () => console.log(`GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}/graphql`));
报错Cannot GET /
我了解到createServer 函数可能已被弃用,但我不知道如何修复它。
【问题讨论】:
-
您在哪里看到此错误?如果您要去
http://localhost:8000/并在浏览器中看到它,那么这是预期的行为——您只为/graphql和/graphiql定义了路由,而不是/。 -
需要详细说明吗?