[英]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.