我正在尝试将我的React Express应用程序部署到Google Clouds App Engine,并且目前出现404状态错误:
无法加载资源:服务器的状态为404()
以及页面上的无法获取/。
在我的本地主机上一切正常,只是在将其部署到Google Cloud时出现此错误。
我的Google控制台日志中还说:.get / home的Hello您好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
App.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;
Server.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}`)
答案 0 :(得分:2)
为您的项目进行构建 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 文件并继续