node.js - 在Google应用程序引擎上部署React Express应用程序: 404 error

标签 node.js reactjs express google-app-engine gcloud

我正在尝试将 React Express 应用部署到 Google 云应用引擎,目前收到 404 状态错误:

加载资源失败:服务器响应状态为404()

以及页面上的 Cannot GET/。

在我的本地主机上一切都运行得很好,只是当我将其部署到谷歌云时出现此错误。

我的谷歌控制台日志还显示:Hello from .get/home undefined undefined undefined

所以

这是我的 yaml 文件:

service: web-form
runtime: nodejs
env: flex

automatic_scaling:
   min_num_instances: 1
   max_num_instances: 1

env_variables:
  PROJECT_ID: 'staging'


handlers:
  - url: /*
    secure: always
    redirect_http_response_code: 301
    script: auto

resources:
  cpu: 1
  memory_gb: 1.7
  disk_size_gb: 10
  volumes:
  - name: ramdisk1
    volume_type: tmpfs
    size_gb: 1

应用程序.js:

import React, { Component } from "react";
import PageOne from "./components/PageOne";
import PageTwo from "./components/PageTwo";
import PageThree from "./components/PageThree";
import PageFour from "./components/PageFour";
import PageFive from "./components/PageFive";
import PageSix from "./components/PageSix";
import { Button } from "semantic-ui-react";
import "semantic-ui-css/semantic.min.css";

import axios from "axios";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      generalDetails: "Text",
      fName: "Text",
      mName: "Text",
      // LName: "Text",
      gender: "Text",
    };

    this.onContentChange = this.onContentChange.bind(this);
    this.onSubmitForm = this.onSubmitForm.bind(this);
  }

  render() {
    return (
      <div className="App">
        <PageOne handleChange={this.onContentChange} />
        <PageTwo handleChange={this.onContentChange} />
        <PageThree handleChange={this.onContentChange} />
        <PageFour handleChange={this.onContentChange} />
        <PageFive handleChange={this.onContentChange} />
        <PageSix handleChange={this.onContentChange} />

        <Button onClick={this.onSubmitForm}
        style={{
          marginLeft: 700,
        }}
        >Submit Form</Button>

        <br />
        <br />
      </div>
    );
  }

  onSubmitForm = e => {
    e.preventDefault();
    var data = {
      generalDetails: this.state.generalDetails,
      fName: this.state.fName,
      mName: this.state.mName,
      lName: this.state.lName,
    };


    axios
  .post("http://localhost:5000/home", data)
  .then(result => {
    console.log(result)
  })
  .catch(() => {
    console.log("Something went wrong. Please try again later");
  });


  };

  //end

  onContentChange(fieldname, data) {
    console.log("On Content Change", data);

    this.setState({
      [fieldname]: data
    });
  }
}

export default App;

服务器.js

const nodemailer = require('nodemailer')
const path = require('path')
const express = require('express')
const app = express()
const http = require('http')
const server = http.createServer(app)
const port = 8080
const cors = require('cors')
app.use(cors())
const bodyParser = require('body-parser')
app.use(bodyParser.json())
const mailgunTransport = require('nodemailer-mailgun-transport')

// to support JSON-encoded bodies
app.use(
  bodyParser.urlencoded({
    // to support URL-encoded bodies
    extended: true
  })
)

app.get('/home', (req, res) => {
  console.log(
    'Hello from .get /home',
    req.body.generalDetails,
    req.body.firstName,
    req.body.mName
  )
})

app.post('/home', function (req, res) {
  const mailgun = require("mailgun-js");



  const DOMAIN = 'domain';
  const mg = mailgun({apiKey: 'mg'
, domain: 'domaing' });
  const message = {
    from: 'Tom <email>',
    to: 'email',
    subject: 'Registration form details',
    html:

    '<h1 style="color:red">Please find new Program orientation registrations details below</h1>' +


    '<p><strong>General Details</strong></p>' +
     '<b> General Details: </b>  ' + '' + req.body.generalDetails +
    '<br> <b>First Name: </b> ' + '' + req.body.fName +
    '<br> <b>Middle Name: </b> ' + '' + req.body.mName +
    '<br> <b>Last Name: </b> ' + '' + req.body.lName

  };

  mg.messages().send(message, function (error, body) {
    console.log(body);
  });

  let data = [
    {
      // page one data
      generalDetails: req.body.generalDetails,
      fName: req.body.fName,
      mName: req.body.mName,
      lName: req.body.lName,
    }
  ]

  res.json(data)
})

app.listen(port, () => `Server running on port ${port}`)

最佳答案

为您的项目构建npm run build,然后像这样创建您的 app.yaml 文件

env: standard    
runtime: nodejs10    
service: default    
handlers:
  - url: /static
    static_dir: build/static

  - url: /assets
    static_dir: build/assets

  - url: /(.*\.(json|ico|js))$
    static_files: build/\1
    upload: build/.*\.(json|ico|js)$

  - url: .*
    static_files: build/index.html
    upload: build/index.html

您可以根据构建文件夹结构修改您的 app.yaml 文件并继续

关于node.js - 在Google应用程序引擎上部署React Express应用程序: 404 error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56284431/

相关文章:

reactjs - Redux-mock-store : Cannot get all dispatched actions into mocked store

ReactJS:Apollo 客户端在用户经过身份验证后设置身份验证 token

javascript - 检测原始数据的变化

javascript - 如何为 Jest 初始化 Feathers 实例一次

javascript - IE11/Edge 中缺少模块导出

javascript - react : uncaught TypeError: Cannot read property 'state' of undefined

mysql - Sequelize findAll 不是一个函数

Angular Express 重定向

javascript - 如何在react js中使用IP获取国家代码和国家名称

javascript - grunt-newer 不使用 grunt-sass