繁体   English   中英

React + Expressjs + App Engine 不工作

[英]React + Expressjs + App Engine not working

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

应用程序.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;

应用程序.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]

包.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 配置

为了使用静态处理程序,至少您的处理程序之一必须包含行脚本:自动部署成功..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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