【问题标题】:404 error with POST request using express server使用快速服务器的 POST 请求出现 404 错误
【发布时间】: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


    【解决方案1】:

    您正在从客户端发出 POST 请求,但未在服务器端配置 POST 请求处理程序。相反,您有一个 GET 请求处理程序。解决方案是为 POST 请求添加处理程序或将 POST 请求方法转换为 GET。

    【讨论】:

      【解决方案2】:

      您没有从 fetchData 函数返回响应。 您应该简单地返回如下响应。 -服务器端也没有发布请求处理程序。 您可以添加为获取请求编写的发布请求处理程序。

      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
      //need to return response as below
       return data.json();
      

      }

      【讨论】:

        【解决方案3】:

        在后端将 app.get 更改为 app.post

        const express = require('express')
        const cors = require('cors')
        
        const app = express()
        app.use(cors())
        app.use(express.json())
        
        // here
        app.post('/api/test', (req: any, res: any) => {
          console.log(req.body)
          res.json({ status: 'ok' })
        })
        
        app.listen(1337, () => {
          console.log('Server started on 1337')
        })
        

        【讨论】:

          【解决方案4】:

          在你没有实现 POST 端点的服务器中,你只实现了 GET 端点

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-02
            • 2021-01-12
            • 2017-06-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多