NodeJS Express的GET请求在移动设备上不起作用。

huangapple go评论64阅读模式
英文:

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 = '&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();
英文:

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(&quot;keypress&quot;) it works on desktop but mobile. So I added OnKeyPress from html inside <input> tag and problem was solved.

huangapple
  • 本文由 发表于 2020年1月3日 21:31:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定