节点服务器中的Response.header错误

时间:2017-12-29 00:24:47

标签: javascript node.js response-headers

我有一个简单的节点服务器。

当我输入' localhost:9015'在我的浏览器中,我收到如下错误。

我检查了代码,发现没有错误。

我做错了什么?

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...

代码:

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}`)
})

0 个答案:

没有答案