好的,由于Cors,我创建了一个代理服务器以连接至supremenewyork.com。某些路由是静态的,例如/ shop或/mobile_stock.json,但其他一些路由是动态的。在转到/mobile_stock.json之后,我从那里解析一个特定的值,然后在下一个路由调用/shop/${item_id}.json中使用该值。但是当我必须通过代理服务器时,我得到404。此item_id一直在变化。做到这一点的任何方法,这是我的代码:

const itemLink = `/shop/${desired_item_id}.json`;

    const itemPage = await helperFunctions.redirectTo(
        itemLink,
        DELAY,
        "Successfully connected to product page!",
        "Error accessing Supreme site, retrying...");


这是helperFunctions.js

const session = axios.create({
    baseURL: `http://botapi.supremenewyork.com:8050`,
  });

// timer function for delays
const timer = ms => new Promise( res => setTimeout(res, ms));

// function for simple get requests
const redirectTo = async (redirectLink, delay, successfullMessage, errorMessage) => {

    while(true){

        try{
            const getRedirect = await session.get(redirectLink);

            if(getRedirect.status === 200){
                if(successfullMessage != null){
                    console.log(successfullMessage);
                }
                await timer(delay);
                return getRedirect;
            }

            else {
                console.log(errorMessage)
                await timer(delay);
            }
        }

        catch(err){
            console.log(err);
            await timer(delay);
        }
    }
}


这是我的server.js,它创建代理服务器:

const express = require('express');
const request = require('request');
const cors = require('cors');

const app = express();

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  next();
});

app.options('*', cors());

app.get('/', (req, res) => {
  request(
    { url: 'https://supremenewyork.com' },
    (error, response, body) => {
      if (error || response.statusCode !== 200) {
        return res.status(500).json({ type: 'error', message: error.message });
      }
      res.send(body);
    }
  )
});
app.get('/mobile_stock.json', (req, res) => {
  request(
    { url: 'https://supremenewyork.com/mobile_stock.json' },
    (error, response, body) => {
      if (error || response.statusCode !== 200) {
        return res.status(500).json({ type: 'error', message: error.message });
      }
      res.send(body);
    }
  )
});

const PORT = process.env.PORT || 8050;
app.listen(PORT, () => console.log(`listening on ${PORT}`));


非常感谢任何帮助,已经取得了很大的进步并学习了很多东西。

最佳答案

在您的路线中,您可以这样定义:

app.get('/:jsonfilename', (req, res) => {
  let jsonName = req.params.jsonfilename
  request(
    { url: 'https://supremenewyork.com/'+jsonName },
    (error, response, body) => {
      if (error || response.statusCode !== 200) {
        return res.status(500).json({ type: 'error', message: error.message });
      }
      res.send(body);
    }
  )
});

09-25 18:36