英文:
NodeJS express get request is not working on mobile
问题
I am developing a web app. I developed everything on local and all the systems is fine. I published the app on digitalocean. When I enter the site from desktop everything is fine but on mobile, express get requests are not working.
this is the code on app.js
const express = require('express');
const app = express();
const fetch = require('node-fetch');
app.listen(3000, () => console.log("Listening at 3000"));
app.use(express.static('public'));
app.use(express.json({limit: '1mb'}));
const hogan = require('hogan.js');
const fs = require('fs');
// GOOGLE PLACES AUTO COMPLETE
app.get('/placesAC/:input', async (request, response) => {
const userInput = request.params['input'];
const placesAPI = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?';
const placeConditions = '®ion=tr';
const APIKey = '&key=MY_API_KEY';
const input = 'input=' + userInput;
const finalURL = placesAPI + input + placeConditions + APIKey;
const fetch_response = await fetch(finalURL);
const json = await fetch_response.json();
response.json(json);
console.log(userInput);
});
and this is how I am fetching it from the client.
const inputVal = fromText.value;
const apiURL = '/placesAC/' + inputVal;
const response = await fetch(apiURL);
const json = await response.json();
英文:
I am developing a web app. I developed everything on local and all the systems is fine. I published the app on digitalocean. When I enter the site from desktop everything is fine but on mobile, express get requests are not working.
this is the code on app.js
const express = require('express');
const app = express();
const fetch = require('node-fetch');
app.listen(3000, () => console.log("Listening at 3000"));
app.use(express.static('public'));
app.use(express.json({limit: '1mb'}));
const hogan = require('hogan.js');
const fs = require('fs');
//GOOGLE PLACES AUTO COMPLETE
app.get('/placesAC/:input', async (request, response) => {
const userInput = request.params['input'];
const placesAPI = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?';
const placeConditions = '&region=tr';
const APIKey = '&key=MY_API_KEY';
const input = 'input=' + userInput;
const finalURL = placesAPI + input + placeConditions + APIKey;
const fetch_response = await fetch(finalURL);
const json = await fetch_response.json();
response.json(json);
console.log(userInput);
});
and this is how I am fetching it from the client.
const inputVal = fromText.value;
const apiURL = '/placesAC/' + inputVal;
const response = await fetch(apiURL);
const json = await response.json();
答案1
得分: 0
尝试在您的API调用中添加try-catch以处理错误,这可能有助于您调试代码中出现的问题。
try {
const apiURL = '/placesAC/' + inputVal;
const response = await fetch(apiURL);
if (!response.ok) // or check for response.status
throw new Error(response.statusText);
const json = await response.json();
} catch (err) {
console.log(err);
}
英文:
For your API call, try to add try-catch to handle the errors,maybe it helps you to debug what's wrong with your code
try {
const apiURL = '/placesAC/'+ inputVal;
const response = await fetch(apiURL);
if (!response.ok) // or check for response.status
throw new Error(response.statusText);
const json = await response.json();
} catch (err) {
console.log(err)
}
答案2
得分: 0
问题出在事件监听器上。当你使用addEventListener("keypress")
时,在桌面上可以正常工作,但在移动设备上不能。所以我在<input>
标签内部添加了OnKeyPress
,问题得以解决。
英文:
The problem was from event listener. When you use addEventListener("keypress")
it works on desktop but mobile. So I added OnKeyPress from html inside <input> tag and problem was solved.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论