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
在我的控制台中,但实际上它从未发生过。
有了200个状态码,我看到客户端可以找到服务器,但控制台消息allo allo从未打印出来。我应该在应用程序中修复什么?