Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind http server with express object to a specific ip address?

How do i bind a specific address to a http object? Currently the default ip address for node.js is 127.0.0.1 and I want to change it to 0.0.0.0

var express = require('express');
var app = express();
var http = require('http').Server(app);

http.listen(3000, "0.0.0.0");

How to achieve that?

edited: I added 0.0.0.0 but the default is still 127.0.0.1

like image 703
airsoftFreak Avatar asked Apr 02 '15 11:04

airsoftFreak


People also ask

How do I find my IP address on Express?

Express provides user's IP information in the request object. If you are using a reverse proxy in the front of Express web server ( You should in the production server ) you can retrieve the IP address in the x-forwared-for header, assuming you have configured your reverse proxy server correctly.

How do I get HTTP server from Express?

The usual two ways to start a server for use with Express are this: var express = require('express'); var app = express(); // Express creates a server for you and starts it var server = app. listen(80);

Is Express server HTTP server?

The Express philosophy is to provide small, robust tooling for HTTP servers, making it a great solution for single page applications, web sites, hybrids, or public HTTP APIs. Navigate to the port 3000 as previously set in the server.

How do I bind http to a host IP address?

For the Domino Web server (HTTP service), you use the Server document to bind HTTP to a host name IP address. On the Internet Protocols > HTTP tab of the Server document, enter one or more IP addresses or FQDNs for the server in the Host name (s) field.

How to bind a specific port in SSL?

We will be focusing on the “ bindings ” area. As SSL is already enabled, you can see the binding for both http and https. Now, if you want additionally bind some specific port, you have to specify port number and other binding information’s. Below is the settings where I used default port “80” and my machine name instead of local host as URL.

Where are IIs site names and bind settings stored?

If you are using a standalone IIS server, the mapping of site names to the server’s IP address is done via the file C:\Windows\system32\drivers\etc\hosts The bind settings are stored in the IIS configuration file <sites> section of the IIS config file C:\Windows\System32\inetsrv\config\applicationHost.config.

Why can’t IIS Express OPEN a website via localhost?

You can easily see this if you open TCPView when your IIS Express is running: As you can see IIS Express is listening for connections via TCPV6 meaning it will not respond if you call it via 127.0.0.1:60000. The address localhost however resolves to an IPv6 address, that’s why your browser is able to open a website via localhost:60000.


1 Answers

0.0.0.0 is not an actual IP that you can reach. Although it means bind to all IPs, or any IP. So no wonder it works for 127.0.0.1. If you have set an IP address on one of your network devices (LAN, WiFi, virtual) it'll listen on those too.


In python you can simply type runserver 0.0.0.0 or something, so in Node.js is there an alternative?

process.argv gives you a list of arguments passed to node.

So if you run

node server.js 0.0.0.0

You'll get

process.argv[0] //=> "node" 
process.argv[1] //=> "server.js" 
process.argv[2] //=> "0.0.0.0" 

Note that it's an array, so as long as you're sure you'll be running your file like that you can use process.argv[2] to specify that as the IP address you want to listen to.

http.listen(3000, process.argv[2]);

Generally though, you should set environment variable IP.

http.listen(3000, process.env[IP]);

Or all of them

http.listen(3000, process.argv[2] || process.env[IP] || "0.0.0.0");
like image 83
laggingreflex Avatar answered Oct 01 '22 07:10

laggingreflex