快递应用服务器.侦听所有接口,而不是仅侦听localhost [英] express app server . listen all interfaces instead of localhost only

查看:102
本文介绍了快递应用服务器.侦听所有接口,而不是仅侦听localhost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这些东西很陌生,并尝试制作一些快递应用

I'm very new for this stuff, and trying to make some express app

var express = require('express');
var app = express();

app.listen(3000, function(err) {
    if(err){
       console.log(err);
       } else {
       console.log("listen:3000");
    }
});

//something useful
app.get('*', function(req, res) {
  res.status(200).send('ok')
});

当我使用以下命令启动服务器时:

When I start the server with the command:

node server.js 

一切都很好.

我在控制台上看到

listen:3000

当我尝试

curl http://localhost:3000

我看到确定".

当我尝试

telnet localhost

我知道

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]' 

但是当我尝试

netstat -na | grep :3000

我知道

tcp  0  0 0.0.0.0:3000   0.0.0.0:*  LISTEN

问题是:为什么它监听所有接口而不是仅监听本地主机?

The question is: why does it listen all interfaces instead of only localhost?

该操作系统是没有任何哨声的linux mint 17.

The OS is linux mint 17 without any whistles.

推荐答案

如果使用,则在调用 app.listen ,服务器将在所有可用接口上运行,即在0.0.0.0

If you use don't specify host while calling app.listen, server will run on all interfaces available i.e on 0.0.0.0

您可以使用以下代码绑定IP地址

You can bind the IP address using the following code

app.listen(3000, '127.0.0.1');

这篇关于快递应用服务器.侦听所有接口,而不是仅侦听localhost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆