简体   繁体   中英

Firebase Cloud function GET Request with google-translate-api

I am new to Node Js trying to create an App translate fr to en. My code work fine when I use it in local node serve or firebase serve, but it didn't work on when i deploy it to Firebase Cloud function.

And also Firebase deploy run but didn't update google API as expected or the word not get translated , it shows "Pls wait" but din't update it Any help will be appreciated.

Firebase LOG - Error: getaddrinfo ENOTFOUND translate.google.com translate.google.com:443 at /user_code/node_modules/google-translate-api/node_modules/google-translate-token/index.js:103:25 at process._tickDomainCallback (internal/process/next_tick.js:135:7)

 const functions = require('firebase-functions'); //in Firebase 

 const functions = require('firebase-functions');


const express = require('express');
var cors = require('cors')
const bodyParser = require('body-parser');
const app = express();
var translator = "Pls Wait";
const translate = require('google-translate-api');

app.use(bodyParser.json());

app.get('/api', function(req , res) {
   createTranslation();
   res.send(translator);
})


function createTranslation() {
     translate('Ik spreek Engels', {to: 'en'}).then(res => {
            console.log(res.text);
            translator = res.text;
            //=> I speak English
        // players = res.text;
            console.log(res.from.language.iso);
         //   return JSON.parse(res.body).data;;
            //=> nl
        }).catch(err => {
            console.log("ERROR");
            console.log(err);
            console.error(err);
        });
  }


var server = app.listen(function(){

    var host = server.address().address;
    var port = server.address().port;

    if(!host || host === "::"){
        host = "localhost:";
    }

    console.log('API running on http://%s%s', host , port);
});



 exports.app = functions.https.onRequest(app); //In Firebase

You can't set up an arbitrary express server using Cloud Functions. This doesn't work:

var server = app.listen(8081, function() { ... })

You have to handle input via one of the triggers defined in the documentation .

Also, you can't just start calling methods at global scope and expect them to work:

translate(...).then(...).catch(...)

Again, you need to think about running your code in response to triggers that you define

You started to define an express app that might respond as an HTTPS trigger, but it calls nothing interesting, and sends only static text to the client:

const app = express();
app.get('/api', cors(), function(req, res) {
    res.end(JSON.stringify(translator));
})
exports.app = functions.https.onRequest(app); //In Firebase

If you're trying to learn Cloud Functions, you should start with a simple example and add to it, rather than trying to port some existing node app verbatim.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM