【发布时间】:2019-11-03 00:23:59
【问题描述】:
我需要访问我的后端 API 以通过电子邮件联系表单发送信息,我将我的应用程序部署在一个名为 Kinghost 的网络主机中,它给了我两个 URL,第一个通常是 mywebaddr.com:port-number,第二个是 mywebaddr.com/site .
我已经尝试将这两个地址与函数路由一起使用,就像我在 localhost 中所做的那样,为了工作,我使用了 http://localhost:4000/contact 例如,但它没有工作......
这是我的要求:
const baseUrl = 'http://mywebsiteurl.com/contact'
const initialState = {
message: {
name: '',
email: '',
subject: '',
main: ''
},
}
export default class ContactUs extends Component {
state = { ...initialState }
reset = () =>{
this.setState({message: initialState.message})
}
send = () => {
const message = this.state.message
const url = baseUrl
console.log(message)
axios.post(url, message)
.then(this.reset())
.then(alert('Message successfully sent!'))
}
这是我的 index.js(后端)
const express = require('express')
const app = express()
const consign = require('consign')
const port = 4005
consign()
.then('./config/middlewares.js')
.then('./api')
.then('./config/routes.js')
.into(app)
app.listen(port, () => {
console.log(port)
})
我的 middlewares.js 包含 cors
const bodyParser = require('body-parser')
const cors = require('cors')
module.exports = app => {
app.use(bodyParser.json())
app.use(cors())
}
实际上,我认为这不是因为我的代码本身,一旦我可以在 localhost 中完美执行所有内容,但不知何故我无法通过正确的 URL 访问
我是 node 新手,我猜不出我做错了什么,所以如果有人能帮助我,我会非常感激 :)
【问题讨论】:
-
您能否发布您正在发出的请求 JS 代码,也许您没有发现错误。可能是 CORS 错误、不正确的标头...太多无法猜测,没有看到任何示例。
-
好的,我已经编辑了我的问题以便提供更多信息!
-
请提供错误消息,我们无法使用“它不起作用”
-
没有任何错误信息!我的日志没有给我任何错误,问题是电子邮件根本没有到达我的邮箱
-
好的,我会尝试在这里发布我的反馈
标签: javascript node.js reactjs api