【问题标题】:Unexpected token on response.writeHeadresponse.writeHead 上的意外令牌
【发布时间】:2013-03-29 16:03:56
【问题描述】:

在 Node.JS 中运行以下代码时会产生一些错误,但我不明白为什么

var http = require("http"), fs = require("fs");

function onRequest(request, response){
        request.on("end", function(){
                fs.readFile("test.txt", "utf-8", function(error, data){
                        response.writeHead(200, {"Content-Type", "text/plain"});
                        data = parseInt(data) + 1;
                        fs.writeFile('test.txt', data);
                        response.end("This page was refreshed "+data+" times!\n");
                });
        });
}

http.createServer(onRequest).listen(8080);

这是我得到的错误:

/home/dev/nodeplay/files.js:7

response.writeHead(200, {"Content-Type", "text-plain"});
                  ^
module.js:434
  var compiledWrapper = runInThisContext(wrapper, filename, true);
                        ^
SyntaxError: Unexpected token ,
    at Module._compile (module.js:434:25)
    at Object..js (module.js:464:10)
    at Module.load (module.js:353:31)
    at Function._load (module.js:311:12)
    at Array.0 (module.js:484:10)
    at EventEmitter._tickCallback (node.js:190:38)

【问题讨论】:

    标签: javascript node.js http fs


    【解决方案1】:

    这部分:{"Content-Type", "text-plain"} 是无效对象。

    你的意思是:

    {"Content-Type": "text-plain"}
    

    【讨论】:

      猜你喜欢
      • 2017-03-29
      • 1970-01-01
      • 2021-03-19
      • 2013-02-11
      • 2016-05-03
      • 2018-04-17
      • 2018-09-11
      • 2011-11-21
      • 2016-06-15
      相关资源
      最近更新 更多