简体   繁体   English

503 服务不可用响应。 如何使用 Heroku 使用服务器端代码中继 API?

[英]503 Service Unavailable response. How can I use Heroku to relay API's using server-side code?

Hi after reading about safe ways to store api keys, I've decided to build my own api relay deployed to heroku using goodreads api as an example. Hi after reading about safe ways to store api keys, I've decided to build my own api relay deployed to heroku using goodreads api as an example. Currently getting this below message from client-side: error from console目前从客户端收到以下消息:来自控制台的错误

and below is my code:以下是我的代码:

const express = require("express");
const fetch = require("node-fetch");
const parseString = require('xml2js').parseString;
const rateLimit = require("express-rate-limit");
const app = express();
const cors = require('cors');
const port = process.env.PORT || 5000;

app.use(cors());

app.set('trust proxy', 1);

const limiter = rateLimit({
  windowMs: 1000, // 1 second
  max: 1, // limit each IP to 1 requests per windowMs
});

app.use(limiter);

app.get("/", (req, res) => res.send("Hello World!"));

app.post("/api/:search", (req, res) => {
   const searchString = `q=${req.query.q}`;
   res.setHeader("Access-Control-Allow-Origin", "*");
   res.setHeader("Access-Control-Allow-Credentials", "true");
   res.setHeader("Access-Control-Allow-Methods", "OPTIONS,POST,GET");
   (async function fetchGoodReads() {
      const response = await fetch(
        `https://www.goodreads.com/search.xml?key=${process.env.GOODREADS_API_KEY}&${searchString}`
     );
     var xml = await response.text();
     parseString(xml, (err, result)=> res.send(result.GoodreadsResponse.search));
   })();
 });

 app.listen(port, () => console.log(`Relay app listening on port ${port}!`));

I've also tried on postman, the response I get is the error page from heroku.我也试过 postman,我得到的响应是来自 heroku 的错误页面。 Any help is welcome:)欢迎任何帮助:)

I had to set my process.env variable on heroku then it worked fine.我必须在 heroku 上设置我的 process.env 变量,然后它工作正常。 Here is where I found how to do that这是我发现如何做到这一点的地方

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

相关问题 如何修复 503(服务不可用)? - How can I fix 503 (service unavailable)? Heroku 503 服务不可用 - Heroku 503 Service Unavailable 我可以使用服务器端Javascript在Razor中对视图进行编码吗? - Can I use server-side Javascript to code views in Razor? 如何将jasmine与服务器端打字稿项目一起使用? - How can I use jasmine with a server-side typescript project? 如何使用jQuery克隆窗口服务器端? - How can I clone a window server-side using jQuery? 我可以在 ES6 中使用服务器端节点 js 并且仍然不使用 bable - Can I use server-side node js with ES6 and still without using bable 我 go 如何在没有服务器端代码的情况下在 OfficeJS 插件中使用 Microsoft Graph? - How would I go about using Microsoft Graph in an OfficeJS addin without server-side code? Heroku:加载资源失败:服务器响应状态为 503(服务不可用)/favicon.ico - Heroku: Failed to load resource: the server responded with a status of 503 (Service Unavailable) /favicon.ico Node / Express - 使用API​​ JSON响应(服务器端)呈现应用程序 - Node/Express - use API JSON response to (server-side) render the app 如何使用 HTTPOnly 访问服务器端的凭据? - How can I access the credentials on the server-side with HTTPOnly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM