簡體   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)

我的應用程序已成功部署在 Heroku 上-當我打開 VSCode 並手動執行 npm 運行啟動時,它可以工作,但是當我關閉 VSCode 時,它不再能夠成功調用后端的任何 API,並且控制台向我顯示了一堆錯誤,例如標題中的那個。

我的控制台(僅在我關閉 VSCode 時發生):

在此處輸入圖像描述

我在后端的代碼:

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 在自己的端口上運行,嘗試像這樣設置端口

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

暫無
暫無

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

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