【问题标题】:Why is my http://localhost:8080 not loading in node.js on mac为什么我的 http://localhost:8080 没有在 mac 上的 node.js 中加载
【发布时间】:2021-07-29 03:42:03
【问题描述】:

所以我对 node.js 很陌生,而且我在 Mac 上。我正在向 W3schools 学习,我必须在本地主机上运行 myfirst.js,但我不断收到此错误:

这个页面不工作 localhost 没有发送任何数据。 ERR_EMPTY_RESPONSE

代码如下:

const https = require('https');
https.createServer(function (req,res){
   res.writeHead(200, {'content-type':'text/html'});
  res.send('Hello world!');
}).listen(8080);

我在终端中运行了这段代码:

Macs-MBP:~ mac$ node /Users/mac/test/myfirst.js

终端命令有效,但是当我进入浏览器并运行时:http://localhost:8080

我只是不断收到上述错误

我该如何解决这个问题?

【问题讨论】:

  • 您需要致电res.end()。发送完所有内容后致电res.end() 或将res.send() 替换为res.end()(只需删除“s”)
  • (Welcome to stackoverflowNode.js已经有好几个版本了,可能会更多:为了Q&A的长效性,请制作习惯于提及使用的相关版本。要问Good Question,请添加您已经使用的文档/资源。)

标签: node.js


【解决方案1】:

试试这样:

const http = require('http');
var server = http.createServer(function (req,res){
  res.writeHead(200, {'content-type':'text/html'});
  res.write('<html><body><p>Hello world!</p></body></html>');
  res.end();
});
server.listen(8080);
console.log('Node.js web server running..')

【讨论】:

  • 谢谢大家,这行得通,我在http上也有和s,正在调用http,所以找不到它。
  • 也有人有任何好的资源来学习 node.js 和反应。
猜你喜欢
  • 2016-04-20
  • 2018-01-27
  • 2013-09-29
  • 2011-01-31
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 2014-11-22
  • 2020-05-26
相关资源
最近更新 更多