我收到以下错误消息:POST http://localhost:8080/api/form 404(未找到)和未捕获(承诺)错误:请求失败,状态码为404
我一直跟随reactjs教程学习,并最终遇到这些错误。我正在使用webpack,而不是create-react-app。救命!
----index.js-file-------
const express = require('express')
const bodyParser = require('body-parser')
const nodemailer = require('nodemailer')
const app= express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.post('api/form', (req, res) => {
console.log(req.body)
})
const PORT = process.env.PORT || 8081
app.listen(PORT, () => {
console.log(`Server listning on port ${PORT} `)
})
-----------contact.js-file-----------------
import React, { Component } from 'react'
import { Form,Button, FormGroup, Label, Input } from 'reactstrap'
import '@babel/polyfill'
import axios from 'axios'
export default class contact extends Component {
constructor() {
super()
this.state = {
name: '',
email: '',
message: ''
}
this.handleChange = this.handleChange.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
}
handleChange = e => {
this.setState({ [e.target.name]: e.target.value })
}
async handleSubmit(e) {
e.preventDefault()
const { name, email, message } = this.state
const form = await axios.post('/api/form', {
name,
email,
message
})
}
---- Chrome ----