node.js 新手,感谢您提供的所有帮助。试图响应变量'words'但是当我启动服务器并转到本地主机时它崩溃并说“TypeError:第一个参数必须是字符串或缓冲区”但是当我尝试将相同的变量写入控制台时它可以工作。谢谢您的帮助!
var http = require("http");
var fs = require('fs');
var text = fs.readFileSync("text.txt").toString();
var words = text.split(/\b/);
function start(){
function onRequest(request, response){
response.writeHead(200, {"Content-type": "text/plain"});
var wordCounts = '';
for(var i = 0; i < words.length; i++)
wordCounts["_" + words[i]] = (wordCounts["_" + words[i]] || 0) + 1;
response.write(words);
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("server has started");
}
exports.start = start;