尝试将我的React Express应用程序部署到GAE,目前,我的/ home路由正常工作并提供“在App引擎标准上的Hello Node.js!”。但是我的张贴路线不起作用,并且收到无法获取/张贴错误。
无法加载资源:服务器的状态为404()
这是我的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 cors = require('cors')
app.use(cors())
const bodyParser = require('body-parser')
app.use(bodyParser.json())
const mailgunTransport = require('nodemailer-mailgun-transport')
const server = app.listen(process.env.PORT || 8081, ()=>{
const host = server.address().address
const port = server.address().port
console.log(`app listening at http://${host}:${port}`);
})
// to support JSON-encoded bodies
app.use(
bodyParser.urlencoded({
// to support URL-encoded bodies
extended: true
})
)
app.get('/home', (req, res) => {
res.send('hello Node.js on App engine standard!')
});
app.post('/post', function (req, res) {
const mailgun = require("mailgun-js");
const DOMAIN = 'mail';
const mg = mailgun({apiKey: 'api'
, domain: 'sandbox89eb814ff83548cebd77c0c7d263172e.mailgun.org' });
const message = {
from: 'Tom <mail>',
to: 'mail',
subject: 'Registration form details',
html:
'<h1 style="color:red">Please find new Program orientation registrations details below</h1>' +
'<p><strong>My Details</strong></p>' +
'<br> <b>Preferred Name: </b> ' + '' + req.body.fName +
'<br> <b>Middle Name: </b> ' + '' + req.body.mName +
'<br> <b>Last Name: </b> ' + '' + req.body.lName +
'<br> <b>Email Name: </b> ' + '' + req.body.email +
};
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,
email: req.body.email,
}
]
res.json(data)
})
// app.listen(port, () => `Server running on port ${port}`)
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 = {
fName: "Text",
lName: "Text",
gender: "Text",
email: '',
};
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 = {
fName: this.state.fName,
mName: this.state.mName,
lName: this.state.lName,
email: this.state.email,
};
axios
.post("http://localhost:8081/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;