【问题标题】:Google App Engine 502 Error: Server Error Node.jsGoogle App Engine 502 错误:服务器错误 Node.js
【发布时间】:2018-11-05 10:21:43
【问题描述】:

我将一个应用程序部署到 google 的 App Engine 服务,这是一个简单的 Express.js 服务器,它为使用 Handlebars.js 呈现的动态页面提供服务。几个小时后一切都很好(它正在为页面提供服务),突然它开始回复这条消息:

502 错误:服务器错误 服务器遇到临时错误,无法完成您的请求。 请在 30 秒后重试。

我尝试了以下所有方法来修复它:

  • 停止/重新启动服务(从控制台)
  • 重新部署(从 cmd 使用命令:"gcloud app deploy"
  • 读取日志(从 cmd 使用:"gcloud app logs read
    • 注意:这表明服务器当前正在侦听 PORT 8080
  • 禁用/启用 App Engine Api(从控制台)
  • 将代码重构为简单的“Hello world”响应并重新部署
  • 完全删除项目并在新项目中重新部署

即使是新项目的答案也是 502。

我的想法:我认为 Google 的代理没有将请求发送到应用程序,因为它实际上正在侦听,但它没有记录请求。

代码如下:

App.js:

    'use strict';

const express = require('express');
const hbs = require('hbs');
const axios = require('axios');
const XLSX = require('xlsx');
const http = require('http');



const port = process.env.PORT || 8080;
const env = process.env.NODE_ENV || 'development';


let app = express();

let urls = {
  url1: **Some url**,
  url2: '**Some url2**'
};


app.set('view engine', 'hbs');

app.use(express.static('public'));    


app.get('/page1', async (req, res)=>{
  console.log('Page 1');
  try{
    let products = await getExcel(urls.url1);
    res.render('page1.hbs', {
      products: products,
    });
  }catch(err){
    console.log(err);
    res.status(500).send('Oops! found an Error.')
  }
});

app.get('/page2', async (req, res)=>{
    console.log('Page 2');
    try{
      let products = await getExcel(urls.url2);
      res.render('page2.hbs', {
        products: products,
      });
    }catch(err){
      console.log(err);
      res.status(500).send('Oops! found an Error.')
    }
 });

app.get('/', (req,res)=>{
  console.log('Home');
  res.render('smx-home.hbs',{});
});


const getExcel = async (url) =>{
**DO SOME STUFF HERE THAT RETURNS AN OBJECT OR AN ERROR** 
}    


app.listen(port, ()=>{
  console.log(`${env} Server listening on port ${port}`);
});

package.json:

{
  "name": "suplementos",
  "version": "1.0.0",
  "description": "Suplementos Cajeme y SuplementosMX",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
  },
  "author": "Wake",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.18.0",
    "express": "^4.16.3",
    "hbs": "^4.0.1",
    "xlsx": "^0.12.13"
  }
}

app.yaml

runtime: nodejs
env: flex

automatic_scaling:
  min_num_instances: 1

resources:
  cpu: 1
  memory_gb: 1
  disk_size_gb: 10

我没有选择...对可能出现的问题有什么想法吗?

奇怪的是,它在开始之前运行了几个小时,现在它只回答 502。

【问题讨论】:

  • 看起来技术支持可以帮助您。您能否使用您的项目编号在 Google 的 Issue Tracker 创建一个帖子。
  • 完成后告诉我,案件编号是多少。

标签: node.js google-app-engine google-cloud-platform


【解决方案1】:

需要在base声明一个路由,返回http状态200。

注意声明路由的顺序。以免有被覆盖的风险。

例子:

    app.get('/', function (req, res) {
      res.status(200).send('Health Check');
    });  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-18
    • 2020-02-20
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    相关资源
    最近更新 更多