繁体   English   中英

如何在我的 Graphql 服务器中添加 cors(Typescript)

[英]How to add cors in my Graphql server(Typescript)

我在我的 graphql 服务器中应用 cors 时遇到问题,因为它不起作用,我研究了很多,但仍然无法正常工作。

这是代码:

 import { BasicServer } from 'realm-object-server' import * as path from 'path' import { GraphQLService } from 'realm-graphql-service'`enter code here` import {cors} from 'cors' const server = new BasicServer() server.addService(new GraphQLService({ // Turn this off in production! disableAuthentication: true })) server.start({ // This is the location where ROS will store its runtime data dataPath: path.join(__dirname, '../data'), middlewares: [cors()] }) .then(() => { console.log(`Realm Object Server was started on ${server.address}`) }) .catch(err => { console.error(`Error starting Realm Object Server: ${err.message}`) })

如何在这个graphql服务器中成功应用cors?

我认为您以错误的方式导入 cors,请尝试以下操作:

 import { BasicServer } from 'realm-object-server' import * as path from 'path' import { GraphQLService } from 'realm-graphql-service'`enter code here` import * as cors from 'cors' // <==== const server = new BasicServer() server.addService(new GraphQLService({ // Turn this off in production! disableAuthentication: true })) server.start({ // This is the location where ROS will store its runtime data dataPath: path.join(__dirname, '../data'), middlewares: [cors()] }) .then(() => { console.log(`Realm Object Server was started on ${server.address}`) }) .catch(err => { console.error(`Error starting Realm Object Server: ${err.message}`) })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM