簡體   English   中英

Express.js-與GraphQL外部API連接

[英]Express.js - Connect with GraphQL external API

我在Express.js上連接到外部API(GraphQL)時遇到問題。 您能給我一些很好的教程嗎?

還有一個問題。 我有這個錯誤:

鏈接到錯誤消息-img

可以與此API連接嗎?

嘗試將以下中間件添加到您的NodeJS / Express應用程序(為方便起見,我添加了一些注釋):

// Add headers
app.use(function (req, res, next) {

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

    // 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();
});

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM