我有服务器端代码,应该返回国家列表。当我使用'npm start'在本地进行部署时,此方法有效,但是当我使用Google Firebase部署项目时,该方法不起作用-./country仅待处理。
这是带有get函数的app.js代码。它适用于/ comments,因为它返回200响应,但/ country返回504错误。
const express = require('express');
const app = express();
const fetch = require('node-fetch');
var cors = require('cors');
var accents = require('remove-accents');
var bodyParser = require('body-parser');
var request = require('request');
app.use(bodyParser.urlencoded({extended:false}));
app.use(cors());
app.use(express.static('client'));
app.get('/country', async function (req, resp){
try{
let country = req.query.country_name;
let response = await fetch('https://restcountries.eu/rest/v2/name/' + accents.remove(country));
if(response.ok){
let body = await response.text();
resp.send(body);
} else {
if (response.code == undefined){
let body = '';
resp.send(body);
}
throw new Error('Problem getting data ' + response.code);
}
} catch (error) {
console.log(error);
}
});
app.get('/comments', async function (req, resp){
try{
let country = req.query.country_name;
let response = await fetch('https://...firebaseio.com/Comments/' + country+'.json');
if(response.ok){
let body = await response.text();
resp.send(body);
} else {
throw new Error('Problem getting data ' + response.code);
}
} catch (error) {
console.log(error);
}
});
const functions = require('firebase-functions')
exports.app = functions.https.onRequest(app);
这是firebase.json文件
{
"hosting": {
"public": "public",
"rewrites": [{
"source": "**",
"function": "app"
}]
}
}