Node.js sets Content-Type

http.createServer((request, response) => {
  response.setHeader("Access-Control-Allow-Origin","*")

  request.on("data", data => {
    console.log(data)
  })

  request.on("end", () => {
    console.log(request)
    response.end("success")

  })

}).listen(8088)

during the request, if the parameter is of String type, it can be successful, but if the request is of Object type, an error will be reported:
Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.
how should I write it

Mar.28,2021

you can handle all requests, but option requests cannot set Content-Type headers. You have to set the cors header separately for the option request, and then set the Content-Type header for the normal request to return the response.


add
response.setHeader ('Access-Control-Allow-Headers','*')

Menu