代码之家  ›  专栏  ›  技术社区  ›  Johhnny

部署的Nodejs Express应用程序未收到来自内置React客户端的请求

  •  0
  • Johhnny  · 技术社区  · 2 年前

    if (process.env.NODE_ENV === "production") {
     app.use(express.static(path.join(__dirname, '../client/dist')));
     app.get("*", (req, res) => {
       res.sendFile(path.resolve(__dirname,  "../client/dist", "index.html"));
     });
    }
    

    这是我的整个服务器索引。js代码

    const express = require('express')
    const app = express();
    const port = 3000;
    const cors =require('cors');
    const request = require('request');
    const words = require('./words');
    require('dotenv').config();
    const path = require('path');
    /*
    const apiKey = process.env.google_place_api_key;
    app.use(cors({
      origin:'http://localhost:8080',
      credentials:true
    }));*/
    const apiKey=process.env.apiKey;
    if (process.env.NODE_ENV === "production") {
      app.use(express.static(path.join(__dirname, '../client/dist')));
      app.get("*", (req, res) => {
        res.sendFile(path.resolve(__dirname,  "../client/dist", "index.html"));
      });
    }
    app.get('/city',(req,res)=>{
      console.log("allo allo");
      const cityname = req.query.name;
      let resArray=[];
      request({
        uri:'https://maps.googleapis.com/maps/api/place/textsearch/json',
        qs:{
          query:"point of interest in"+cityname,
          key:apiKey
        }
      },(err,request,body)=>{
        const object = JSON.parse(body);
        let photo;
        let placeObject={
          placeName:'',
          photo:''
        }
        for(let i=0;i<object.results.length;i++){
          if(object.results[i].photos!==undefined){
            placeObject['placeName']=words.returnWordsInEnglish(object.results[i].name);
            placeObject['photo'] =object.results[i].photos[0]['photo_reference'];
          }
          
      
          //getImageOfPlace(photo);
          resArray[i]=Object.assign({},placeObject);
        }
        res.send(resArray);
      });
     });
    
    app.listen(process.env.PORT||port, () => {
      console.log(`Example app  listening on port ${process.env.PORT||port}!`)
    });
    
    async function getImageOfPlace(place){
      try{
        const response = await fetch('https://maps.googleapis.com/maps/api/place/photo? maxwidth=400&photo_reference='+place+'&key='+apiKey);
        //const json = await response.json();
        console.log(response);
      }catch(e){
        console.log(e);
      }
    }
    

    我正在向Nodejs服务器发送just fetch请求

    fetch("myherokusite.com/city?name="+searchValue,{
     method:'GET',
     credentials:'include',
     headers:{
      'Accept':'application/json',
      'Content-Type':'application/json'
     }
    }).then((data:any)=>{
        console.log("data");
        console.log(data);
        return data.json();
    }).then(data=>{
        console.log(data);
        setResult(Object.entries(data));
    })
    

    /city?name="Madrid" allo allo 在我的控制台中,但实际上它从未发生过。

    enter image description here

    有了200个状态码,我看到客户端可以找到服务器,但控制台消息allo allo从未打印出来。我应该在应用程序中修复什么?

    1 回复  |  直到 2 年前
        1
  •  1
  •   derpirscher    2 年前

    路由按创建顺序匹配。您创建 app.get('*', ...)

    将路线移动到最后一条。当所有其他路线不匹配时,作为后备