Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  25] [ 2]  / answers: 1 / hits: 20648  / 10 Years ago, tue, march 4, 2014, 12:00:00

I was about teaching my friend an intro to node but then
I wonder why this code from nodejs.org:



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


when hosted, it doesn't accessible from public ip (it's still accessible from localhost though)
while this code from express.js:



var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 80);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});


does. Please help me modify the basic code the one from nodejs's homepage so that it become accessible from public ip so I can demonstrate it to my friend in a very basic way. The fresh code generated by express.js worked just fine.



What am I missing here?


More From » node.js

 Answers
46

As per the server.listen docs,




Begin accepting connections on the specified port and hostname. If the
hostname is omitted, the server will accept connections directed to
any IPv4 address (INADDR_ANY).




To make it accept connections from all ips (0.0.0.0), change it to read like this



}).listen(80); // No explicit ip, defaults to all ips 0.0.0.0
console.log('Server running in port 80');

[#72176] Sunday, March 2, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daijab

Total Points: 60
Total Questions: 99
Total Answers: 110

Location: Bosnia and Herzegovina
Member since Thu, Jun 24, 2021
3 Years ago
;