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

nimble-restivus package does not work in Meteor GraphQL

我在 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>

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