【发布时间】:2022-01-20 13:09:27
【问题描述】:
我正在运行这个函数,它应该将数据发布到我的快速服务器。单击按钮时调用该函数。
const fetchData = async () => {
const response = await fetch('http://localhost:1337/api/test', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: 'hello world'
}),
})
// const data = await response.json()
const data = await response
console.log(data)
}
这是我的快速配置
const express = require('express')
const cors = require('cors')
const app = express()
app.use(cors())
app.use(express.json())
app.get('/api/test', (req: any, res: any) => {
console.log(req.body)
res.json({ status: 'ok' })
})
app.listen(1337, () => {
console.log('Server started on 1337')
})
问题是,当我单击按钮时,我收到 POST 请求的 404 错误,而我的 console.log(response) 导致以下结果。
Response { type: "cors", url: "http://localhost:1337/api/test", redirected: false, status: 404, ok: false, statusText: "Not Found", headers: Headers, body: ReadableStream, bodyUsed: false }
body: ReadableStream { locked: false }
bodyUsed: false
headers: Headers { }
ok: false
redirected: false
status: 404
statusText: "Not Found"
type: "cors"
url: "http://localhost:1337/api/test"
<prototype>: ResponsePrototype { clone: clone(), arrayBuffer: arrayBuffer(), blob: blob(), … }
【问题讨论】:
标签: javascript node.js express cors