nimble-restivus包在Meteor GraphQL中不起作用

时间:2017-08-10 05:44:49

标签: api meteor graphql nimble restivus

我在Meteor服务器中设置了GraphQL,并使用nimble-restivus包创建了一个简单的API。但我不能打电话给这个API。请帮忙。

这是API设置:

  var Api = new Restivus({
    useDefaultAuth: true,
    prettyJson: true,
  });
  Api.addRoute('status', {
    post: function() {
      return { status: 'OK' };
    },
  });

这是测试api的命令行:

curl -X POST http://localhost:3000/api/status

这是Meteor服务器上的配置GraphQL:

import { Meteor } from 'meteor/meteor';
import express from 'express';
import {
  graphqlExpress,
  graphiqlExpress,
} from 'graphql-server-express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { execute, subscribe } from 'graphql';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { schema } from './schema';

const server = express();

const whitelist = [ 'http://localhost:3000' ];
const corsOptions = {
  origin(origin, callback){
    const originIsWhitelisted = whitelist.indexOf(origin) !== -1;
    callback(null, originIsWhitelisted);
  },
  credentials: true,
};
server.use(cors(corsOptions));
// server.use('*', cors({ origin: Meteor.settings.GraphQL.clientURL }));
server.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
server.use('/graphiql', graphiqlExpress({
  endpointURL: '/graphql',
  subscriptionsEndpoint: 'ws://localhost:3000/subscriptions',
}));

// We wrap the express server so that we can attach the WebSocket for subscriptions
const ws = createServer(server);
ws.listen(3000, () => {
  console.log('GraphQL Server is running');
  // Set up the WebSocket for handling GraphQL subscriptions
  new SubscriptionServer({
    execute,
    subscribe,
    schema
  }, {
    server: ws,
    path: '/subscriptions',
  });
});

这是运行命令后的结果:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /api/status</pre>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我修好了。原因是Meteor服务器和GraphQL在同一个端口(端口3000)中运行。我们需要将GraphQL的端口更改为其他端口(例如:3002等)