2016-12-12 15 views
0

ノードjsでサーバーを実行しようとしています。実際にはグラフサーバーです。ローカルホストで3000、およびサーバ側:ローカルホストで実行中のJS405エラーが発生する、ノードjs

クライアント側の反応から私は、クライアント側からのメソッドを呼び出しています4000

クライアント側のコードを

var dice = 3; 
var sides = 6; 
var xhr = new XMLHttpRequest(); 
xhr.responseType = 'json'; 
xhr.open("POST", "http://localhost:4000/graphql"); 
xhr.setRequestHeader("Content-Type", "application/json"); 
xhr.setRequestHeader("Accept", "application/json"); 
xhr.onload = function() { 
    debugger; 
    console.log('data returned:', xhr.response); 
} 
var query = `query RollDice($dice: Int!, $sides: Int) { 
    rollDice(numDice: $dice, numSides: $sides) 
}`; 
xhr.send(JSON.stringify({ 
    query: query, 
    variables: { dice: dice, sides: sides }, 
})); 

とサーバーで側、私は以下のコードを使用しています

var express = require('express'); 
var graphqlHTTP = require('express-graphql'); 
var { buildSchema } = require('graphql'); 

// Construct a schema, using GraphQL schema language 
var schema = buildSchema(` 
    type Query { 
    rollDice(numDice: Int!, numSides: Int): [Int] 
    } 
`); 

// The root provides a resolver function for each API endpoint 
var root = { 
    rollDice: function ({numDice, numSides}) { 
    var output = []; 
    for (var i = 0; i < numDice; i++) { 
     output.push(1 + (numSides *3)); 
    } 
    return output; 
    } 
}; 

var app = express(); 
//CORS middleware 
app.use(function (req, res, next) { 

    // Website you wish to allow to connect 
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000'); 

    // Request methods you wish to allow 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); 

    // Request headers you wish to allow 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); 

    // Set to true if you need the website to include cookies in the requests sent 
    // to the API (e.g. in case you use sessions) 
    res.setHeader('Access-Control-Allow-Credentials', true); 

    // Pass to next layer of middleware 
    next(); 
}); 

app.use('/graphql', graphqlHTTP({ 
    schema: schema, 
    rootValue: root, 
    graphiql: true, 
})); 
app.listen(4000); 
console.log('Running a GraphQL API server at localhost:4000/graphql'); 

私は取得していますエラーが

です

XMLHttpRequestはhttp://localhost:4000/graphqlをロードできません。プリフライトの応答が無効なHTTPステータスコード405

を持っている私が行方不明です何...

おかげであります。

答えて

1

あなたは正しくOPTIONS要求に応答して、CORSコード内の飛行前のリクエスト(OPTIONS)を処理する必要があります。

function (req, res, next) { 

    // Website you wish to allow to connect 
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000'); 

    // Request methods you wish to allow 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); 

    // Request headers you wish to allow 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); 

    // Set to true if you need the website to include cookies in the requests sent 
    // to the API (e.g. in case you use sessions) 
    res.setHeader('Access-Control-Allow-Credentials', true); 

    // Pass to next layer of middleware 

    if (req.method === "OPTIONS") { 
     return res.status(200).end(); 
    } 

    return next(); 
} 
関連する問題