只能从机器 运行 服务器访问 Express Node.js 服务器
Can only access Express Node.js server from machine running server
我在使用 express 时遇到了新手问题。我正在使用 Ubuntu 14.04 并在终端中创建一个新目录和 运行 "express" 并设置一个项目模板。然后运行"npm install"安装依赖。然后编辑 'views/layout.jade' 来改变 "!!!" "doctype html" 作为节点错误提示尝试 运行 服务器而不更改。
之后,我通过在终端上输入 "node app.js" 来启动服务器,然后可以使用 "localhost:3000" 或“192.168.1.13:3000”在我的浏览器中看到默认页面。
我的理解是我应该可以从本地网络中的另一台计算机使用“192.168.1.13:3000”。然而,当我尝试这个时,我得到 "connecting to 192.168.1.13..." 大约 30 秒到一分钟然后它说 "The connection has timed out ... The server at 192.168.1.13 is taking too long to respond."
我已经在 windows 桌面和 android phone 上从 firefox 和 chrome 尝试过这个。我还尝试将其设置为端口 80 而不是 3000,结果相同。我尝试在 app.listen 中的端口后添加“'0.0.0.0'”,但这也有相同的结果。
我从未在此 Ubuntu 安装中设置或搞乱过防火墙或端口转发,所以我认为这些不应该是问题所在? (是不是我路由器的问题?)可能我这里说错了。
这是相关文件-
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.玉:
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body!= body
views/index.玉:
h1= title
p Welcome to #{title}
如果我遗漏了什么,请告诉我。干杯。
我按照 slebetman 的建议通过使用 iptables 打开传入端口 3000 解决了这个问题,使用以下命令:
iptables -I INPUT 1 -p tcp --dport 3000 -j ACCEPT
我在使用 express 时遇到了新手问题。我正在使用 Ubuntu 14.04 并在终端中创建一个新目录和 运行 "express" 并设置一个项目模板。然后运行"npm install"安装依赖。然后编辑 'views/layout.jade' 来改变 "!!!" "doctype html" 作为节点错误提示尝试 运行 服务器而不更改。
之后,我通过在终端上输入 "node app.js" 来启动服务器,然后可以使用 "localhost:3000" 或“192.168.1.13:3000”在我的浏览器中看到默认页面。
我的理解是我应该可以从本地网络中的另一台计算机使用“192.168.1.13:3000”。然而,当我尝试这个时,我得到 "connecting to 192.168.1.13..." 大约 30 秒到一分钟然后它说 "The connection has timed out ... The server at 192.168.1.13 is taking too long to respond."
我已经在 windows 桌面和 android phone 上从 firefox 和 chrome 尝试过这个。我还尝试将其设置为端口 80 而不是 3000,结果相同。我尝试在 app.listen 中的端口后添加“'0.0.0.0'”,但这也有相同的结果。
我从未在此 Ubuntu 安装中设置或搞乱过防火墙或端口转发,所以我认为这些不应该是问题所在? (是不是我路由器的问题?)可能我这里说错了。
这是相关文件-
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.玉:
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body!= body
views/index.玉:
h1= title
p Welcome to #{title}
如果我遗漏了什么,请告诉我。干杯。
我按照 slebetman 的建议通过使用 iptables 打开传入端口 3000 解决了这个问题,使用以下命令:
iptables -I INPUT 1 -p tcp --dport 3000 -j ACCEPT