gpt4 book ai didi

javascript - response.writeHead 上出现意外标记

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:39 26 4
gpt4 key购买 nike

在 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)

最佳答案

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

你的意思是:

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

关于javascript - response.writeHead 上出现意外标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15699508/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com