我在尝试启动Node.js时正在查看
this帖子,我开始使用
this guide来学习基础知识.
我的服务器的代码是:
var http = require('http'); http.createServer(function (request,response) { request.on('end',function() { response.writeHead(200,{ 'Content-Type' : 'text/plain' }); response.end('Hello HTTP!'); }); }).listen(8080);
当我去localhost:8080(根据指南),我得到一个’没有数据收到’错误.我看到一些页面说https://是必需的,但是会返回’SSL Connection Error’.我无法弄清楚我错过了什么.
解决方法
您的代码中的问题是“end”事件永远不会被触发,因为您正在使用Stream2请求流,就好像它是Stream1一样.阅读迁移教程 –
http://blog.nodejs.org/2012/12/20/streams2/
要将其转换为“旧模式流行为”,您可以添加“data”事件处理程序或“.resume()”调用:
var http = require('http'); http.createServer(function (request,response) { request.resume(); request.on('end',function() { response.writeHead(200,{ 'Content-Type' : 'text/plain' }); response.end('Hello HTTP!'); }); }).listen(8080);
如果您的示例是http GET处理程序,则您已经拥有所有标头,并且不需要等待正文:
var http = require('http'); http.createServer(function (request,response) { response.writeHead(200,{ 'Content-Type' : 'text/plain' }); response.end('Hello HTTP!'); }).listen(8080);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。