Response.header error in node server

邮差的信 提交于 2020-03-05 05:42:30

问题


I have a simple node server.

When I type 'localhost:9015' in my browser I get the error showing below.

I have checked the code and I found no error.

What am I doing wrong?

server is listening on 9015
/
C:\nodeapp\cloudnh_v2\teste.js:41
   response.header("Access-Control-Allow-Origin", "*");
            ^

TypeError: response.header is not a function
    at Server.requestHandler (C:\nodeapp\cloudnh_v2\teste.js:41:13)
    at emitTwo (events.js:125:13)
    at Server.emit (events.js:213:7)
    at parserOnIncoming (_http_server.js:602:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:116:23)
[nodemon] app crashed - waiting for file changes before starting...

Code:

const http = require('http')
const port = 9015

const requestHandler = (request, response) => {
   console.log(request.url)
   response.header("Access-Control-Allow-Origin", "*");
   response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
   response.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
   response.header("Cache-Control", "no-cache");

  response.end('Hello Node.js Server!')
}

const server = http.createServer(requestHandler)

server.listen(port, (err) => {
  if (err) {
    return console.log('something bad happened', err)
  }

  console.log(`server is listening on ${port}`)
})

来源:https://stackoverflow.com/questions/48016014/response-header-error-in-node-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!