React + Expressjs + App Engine无法正常工作

时间:2019-12-05 13:04:20

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

我有一个带有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完成。

我想念什么?

1 个答案:

答案 0 :(得分:0)

我注意到的一件事是,script: auto部分中没有任何handlers。根据{{​​3}}:

  

要使用静态处理程序,您的处理程序中至少有一个必须包含以下行脚本:自动才能成功部署。.