无法运行测试node.js http脚本:找不到连接

时间:2022-08-03 21:58:06

In Kubuntu I have installed node.js sudo apt-get install nodejs

在Kubuntu我安装了node.js sudo apt-get install nodejs

Then I made a js file called example.js containing

然后我创建了一个名为example.js的js文件

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Then I ran the file with node example.js

然后我用节点example.js运行该文件

but I do not see the log

但是我没有看到日志

In the browser I get a error saying connection not found.

在浏览器中,我收到一条错误消息,指出未找到连接。

if I ping http://127.0.0.1 then I get a error saying unknown host

如果我ping http://127.0.0.1然后我收到错误说未知主机

what am I doing wrong? have I installed node.js incorrectly?

我究竟做错了什么?我错误地安装了node.js吗?

1 个解决方案

#1


1  

Since Ubuntu and its flavors already have a node, Node.js goes by another name on these systems:

由于Ubuntu及其风格已经有一个节点,Node.js在这些系统上有另一个名称:

There is a naming conflict with the node package (Amateur Packet Radio Node Program), and the nodejs binary has been renamed from node to nodejs.

与节点包(业余分组无线电节点程序)存在命名冲突,并且nodejs二进制文件已从节点重命名为nodejs。

So, at least on that machine, you'll have to instead use:

所以,至少在那台机器上,你必须改为使用:

nodejs example.js

Also, you might use cURL instead to test as ping isn't an HTTP client.

此外,您可以使用cURL来测试,因为ping不是HTTP客户端。

curl http://127.0.0.1:1337/

And, as others have already noted, you'll have to include the specified port (.listen(1337,) in the URL since it's non-default.

并且,正如其他人已经注意到的那样,您必须在URL中包含指定的端口(.listen(1337,),因为它是非默认的。

#1


1  

Since Ubuntu and its flavors already have a node, Node.js goes by another name on these systems:

由于Ubuntu及其风格已经有一个节点,Node.js在这些系统上有另一个名称:

There is a naming conflict with the node package (Amateur Packet Radio Node Program), and the nodejs binary has been renamed from node to nodejs.

与节点包(业余分组无线电节点程序)存在命名冲突,并且nodejs二进制文件已从节点重命名为nodejs。

So, at least on that machine, you'll have to instead use:

所以,至少在那台机器上,你必须改为使用:

nodejs example.js

Also, you might use cURL instead to test as ping isn't an HTTP client.

此外,您可以使用cURL来测试,因为ping不是HTTP客户端。

curl http://127.0.0.1:1337/

And, as others have already noted, you'll have to include the specified port (.listen(1337,) in the URL since it's non-default.

并且,正如其他人已经注意到的那样,您必须在URL中包含指定的端口(.listen(1337,),因为它是非默认的。