简体   繁体   English

Node.js 中的类型错误

[英]TypeError in Node.js

My problem is that I want to show the content of a .txt file in the browser using JavaScript (specifically Node.js), but when I try to run the code, it shows TypeError: Header name must be a valid HTTP Token ["Content-Type "] .我的问题是我想使用 JavaScript(特别是 Node.js)在浏览器中显示.txt文件的内容,但是当我尝试运行代码时,它显示TypeError: Header name must be a valid HTTP Token ["Content-Type "] The code is following (I have seen this code in a tutorial):代码如下(我在教程中看到过这段代码):

var http=require('http');
var fs=require('fs');

var server=http.createServer(function(req,res){
    console.log("Request was made" + req.url);
    res.writeHead(200, {'Content-Type ': 'text/plain'});
    var myReadStream=fs.createReadStream(__dirname + '/readMe.txt', 'utf8');
    myReadStream.pipe(res);

    });
    server.listen(3000, '127.0.0.1');
    console.log('Listening to port 3000');

And specifically, it points out the mistake is in this part of the code:具体来说,它指出错误出在这部分代码中:

res.writeHead(200, {'Content-Type ': 'text/plain'});

I would like to know what is the problem.我想知道是什么问题。

You have an space here:你在这里有一个空间:
在此处输入图片说明

Just remove the space: 'Content-Type': instead of 'Content-Type ':只需删除空格: 'Content-Type':而不是'Content-Type ':

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM