【问题标题】:React + Expressjs + App Engine not workingReact + Expressjs + App Engine 不工作
【发布时间】:2019-12-05 13:04:20
【问题描述】:

我有一个带有 expressjs 的应用程序,并在 Google App Engine 上运行。 这是我的配置:

app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cors = require("cors");
const apiVersion = "/api"
var indexRouter = require('./routes/index');
var projectsRouter = require('./routes/projects');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(cors());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'client','build')));

app.use(apiVersion+'/', indexRouter);
app.use(apiVersion+'/projects', projectsRouter);
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'))
})
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

app.yaml:

# [START runtime]
runtime: nodejs10
instance_class: F1
automatic_scaling:
  max_idle_instances: 1
  max_instances: 2
# [END runtime]

# [START handlers]
handlers:
 - url: /
   static_files: client/build/index.html
   upload: client/build/index.html
   secure: always
   redirect_http_response_code: 301

 - url: /(.*)
   static_files: client/build/\1
   upload: client/build/(.*)
   secure: always
   redirect_http_response_code: 301
# [END handlers]

package.json

{
  "name": "api",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "client": "npm run local --prefix client",
    "server": "nodemon ./bin/www",
    "dev": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\"",
    "build": "npm run build --prefix client"
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "cors": "^2.8.5",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "jade": "~1.11.0",
    "morgan": "~1.9.1",
  },
  "devDependencies": {
    "concurrently": "^5.0.0",
    "nodemon": "^2.0.1"
  }
}

我设法通过 npm run dev 让它在本地环境中运行,但是当我构建 react 应用并将其部署到 App Engine 上时,站点已正确加载,但每个表达 api 的请求都以 404 结束。

我错过了什么?

【问题讨论】:

  • 我注意到的一件事是您的handlers 部分中没有任何script: auto。根据app.yaml configurationIn order to use static handlers, at least one of your handlers must contain the line script: auto to deploy successfully.。您似乎在本地同时运行两个实例。如果您愿意,也可以使用 Dockerfile 在云上类似地运行两者,在这种情况下,您必须切换到 Flex 或使用其他产品,例如 Cloud Run
  • 嗨@JKleinne我设法修复它添加脚本:在app.yaml中自动,不需要使用dockerfile。
  • 如果您添加响应,我会将其设置为解决方案。
  • 很高兴它最终成功了。我在下面添加了我的答案,谢谢

标签: node.js express google-app-engine


【解决方案1】:

我注意到的一件事是,您的 handlers 部分中没有任何 script: auto。根据app.yaml configuration

为了使用静态处理程序,至少有一个处理程序必须包含行 script: auto 才能成功部署..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2012-06-13
    • 2021-12-25
    • 2014-09-22
    • 1970-01-01
    • 2011-08-06
    相关资源
    最近更新 更多