qk4
qk4

Reputation: 91

Can only access Express Node.js server from machine running server

I'm having a newbie problem with using express. I'm using Ubuntu 14.04 and make a new dir and run "express" in the terminal and it sets up a project template. Then run "npm install" to install the dependencies. And then edit 'views/layout.jade' to change "!!!" to "doctype html" as the node error suggests when trying to run the server without change.

Afterward, I start the server by entering "node app.js" on terminal and am then able to see the default page in my browser by using "localhost:3000" or "192.168.1.13:3000".

My understanding is that I should be able to use "192.168.1.13:3000" from another computer in the local network. However when I try this I get "connecting to 192.168.1.13..." for about 30 seconds to a minute then it says "The connection has timed out ... The server at 192.168.1.13 is taking too long to respond."

I've tried this from firefox and chrome on both windows desktop and android phone. I also tried setting it to port 80 instead of 3000 with the same result. I've tried adding "'0.0.0.0'," after the port in app.listen but this has same result as well.

I have never set up or messed around with firewalls or port forwarding in this Ubuntu installation so I believe those shouldn't be the issue? (is the problem with my router?) Maybe I'm wrong here.

Here's the relevant files-

app.js:

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

app.listen(3000, function(){
  console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});

routes/index.js:

/*
 * GET home page.
 */

exports.index = function(req, res){
  res.render('index', { title: 'Express' })
};

views/layout.jade:

doctype html
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
  body!= body

views/index.jade:

h1= title
p Welcome to #{title}

Let me know if I've left anything out. Cheers.

Upvotes: 1

Views: 1259

Answers (1)

qk4
qk4

Reputation: 91

I resolved this by opening incoming port 3000 with iptables as suggested by slebetman, with the following command:

iptables -I INPUT 1 -p tcp --dport 3000 -j ACCEPT

Upvotes: 4

Related Questions