简体   繁体   English

应用程序部署在 Heroku 但 api 调用现在失败(无法加载资源:net::ERR_CONNECTION_REFUSED,TypeError:无法获取)

[英]App deployed on Heroku but api calls are now failing (Failed to load resource: net::ERR_CONNECTION_REFUSED, TypeError: Failed to fetch)

My app is successfully deployed on Heroku- it works when I have VSCode open and do npm run start manually, however when I close VSCode it is no longer able to successfully call any APIs on the backend and the console shows me a bunch of errors like the one in the title.我的应用程序已成功部署在 Heroku 上-当我打开 VSCode 并手动执行 npm 运行启动时,它可以工作,但是当我关闭 VSCode 时,它不再能够成功调用后端的任何 API,并且控制台向我显示了一堆错误,例如标题中的那个。

my console (ONLY happens when I close VSCode):我的控制台(仅在我关闭 VSCode 时发生):

在此处输入图像描述

my code in the backend:我在后端的代码:

const PORT = 8000
const express = require('express')
const cors = require('cors')
const {TwitterApi} = require('twitter-api-v2')
const axios = require('axios')
const cheerio = require('cheerio')
require('dotenv').config()
const snoowrap = require('snoowrap')
const linkPreviewGenerator = require('link-preview-generator')
const spotify = require('spotify-web-api-node')
const fetch = require('node-fetch')
var request = require('request')

const app = express()
app.use(cors())


app.get('/', async (req, res) => {

    const client = new TwitterApi(process.env.twt_bearer_token)

    const trendsInternational = await client.v1.trendsByPlace(1);

    const trendList = []

    for (const {trends} of trendsInternational) {
        for (const trend of trends) {
            trendList.push({
                name: trend.name,
                url: trend.url
            })
        }
    }

    res.json(trendList)
})



app.get('/reddit', async (req, res) => {
    const r = new snoowrap({
        userAgent: process.env.user_agent,
        clientId: process.env.client_id,
        clientSecret: process.env.client_secret,
        refreshToken: process.env.refresh_token
    })

    topPosts = []
    
    ;(await r.getHot()).forEach(post => {
        topPosts.push({
            title: post.title,
            url: post.url
        })
    })
    
    res.json(topPosts);

})

app.get('/news', async (req, res) => {
    const news_url = 'https://www.theguardian.com/international'

    axios(news_url)
    .then(response => {
        const html = response.data;
        const $ = cheerio.load(html);
        const articles = [];
        const values = new Set();

        $('.fc-item__title', html).each(function () { //<-- cannot be a function expression

            const title = $(this).text().trim();
            const url = $(this).find('a').attr('href');

            if (!values.has(url)) {

              values.add(url);

              articles.push({
                  title,
                  url
              });
            }  
        })
        res.json(articles)
    }).catch(err => console.log(err))


})

app.listen(PORT, () => console.log(`Server is running on port ${PORT}`))```

Heroku runs on its own port, try setting port like this Heroku 在自己的端口上运行,尝试像这样设置端口

const PORT = Number(process.env["PORT"]) || 8000

暂无
暂无

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

相关问题 在看似没有改变之后,正在工作的 function 现在正在加载资源失败:net::ERR_CONNECTION_REFUSED - After changing seemingly nothing, a function that was working is now getting Failed to load resource: net::ERR_CONNECTION_REFUSED AxiosError:加载资源失败:net::ERR_CONNECTION_REFUSED - AxiosError : Failed to load resource: net::ERR_CONNECTION_REFUSED Webkit浏览器无法加载资源:net :: ERR_CONNECTION_REFUSED - Webkit Browsers Failed to load resource: net::ERR_CONNECTION_REFUSED Heroku + Node.js + Peer.js(webrtc):无法加载资源:net :: ERR_CONNECTION_REFUSED - Heroku + Node.js + Peer.js (webrtc): Failed to load resource: net::ERR_CONNECTION_REFUSED 无法加载资源:net::ERR_CONNECTION_REFUSED 仅适用于来自 instagram API 的选择性图像 - Failed to load resource: net::ERR_CONNECTION_REFUSED for only selective images from instagram API JavaScript fetch ERR_CONNECTION_REFUSED vs TypeError 无法获取 - JavaScript fetch ERR_CONNECTION_REFUSED vs TypeError Failed to fetch 发出GET请求时出错“无法加载资源:net :: ERR_CONNECTION_REFUSED” - error when making GET request “Failed to load resource: net::ERR_CONNECTION_REFUSED” 从本地主机获取资源失败:net :: ERR_CONNECTION_REFUSED - Failed to load resource: net::ERR_CONNECTION_REFUSED when fetching from localhost 无法加载资源:Net::ERR_CONNECTION_REFUSED on NodeJs ,AngularJs Web 应用程序 - Failed to load resource: net::ERR_CONNECTION_REFUSED on NodeJs ,AngularJs Web Application 这个问题的解决方案是什么“加载资源失败:net::ERR_CONNECTION_REFUSED http://localhost:8989/route?.....” - what is the solution to this problem "failed to load resource: net::ERR_CONNECTION_REFUSED http://localhost:8989/route?....."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM