NodeJS express get request is not working on mobile

0 votes

I'm working on a web application. Everything was developed locally, and everything works perfectly. I made the app available on digitalocean. Everything works properly when I access the site from a desktop computer, but express get requests do not work on mobile devices.

This is the app.js code.

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);
});

I'm getting it from the client in this manner.

 const inputVal = fromText.value;
    const apiURL = '/placesAC/' + inputVal;
    const response = await fetch(apiURL);
    const json = await response.json();

Jun 7, 2022 in Node-js by Vaani
• 7,070 points
1,942 views

1 answer to this question.

0 votes

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)
}

To know more about Node JS, It's recommended to join Node JS Certification today.

answered Jun 7, 2022 by Neha
• 9,020 points

Related Questions In Node-js

0 votes
1 answer

How to get GET (query string) variables in Express.js on Node.js?

Hello @kartik, Since you've mentioned Express.js in your ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
3,256 views
0 votes
1 answer

How to get path from the request in nodejs?

Hello @kartik, Try this out: var http = require('http'); var ...READ MORE

answered Oct 14, 2020 in Node-js by Niroj
• 82,840 points
4,517 views
0 votes
1 answer

How to get Node.JS Express to listen only on localhost?

Hello @kartik, This is a bug in hive-go that only ...READ MORE

answered Oct 16, 2020 in Node-js by Niroj
• 82,840 points
7,361 views
0 votes
1 answer
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

Start script missing error when running npm start

It seems that there is an undefined ...READ MORE

answered Feb 10, 2022 in Java by Soham
• 9,710 points
4,438 views
0 votes
1 answer

NodeJS express get request is not working on mobile

For your API call, try to add ...READ MORE

answered Jun 9, 2022 in Node-js by Neha
• 9,020 points
532 views
0 votes
1 answer

What is the role of Nodejs and Express in a MERN stack web application when GraphQL is also used?

Node.js is a JavaScript runtime environment, which ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
2,637 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP