Node.js在locahost上工作,但不能在LAN上工作
我在nodejs中是新的。 我做了简单的ajax教程,它在localhost上工作,但不能在局域网上工作。 我已经在服务器端安装了nodejs。 请帮助我如何解决它。
server.js
var http = require('http'); http.createServer(function (req, res) { console.log('request received'); res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello world......'); }).listen(8080, 'lan_ip');
的index.html
$(document).ready(function() { $.ajax({ url: 'http://lan_ip:8080/', dataType: "jsonp", jsonpCallback: "_testcb", cache: false, timeout: 5000, success: function(data) { $("#test").append(data); }, error: function(jqXHR, textStatus, errorThrown) { alert('error ' + textStatus + " " + errorThrown); } }); });
我的文件目录像
wamp_server> nodetutorial
- server.js - index.html
你在某种程度上被防火墙挡住了。 你的代码是完全有效的。
你可能想尝试作为一个testing是这样的:
var http = require('http'); http.createServer(function (req, res) { console.log('request received'); res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello world......'); }).listen(8080, '0.0.0.0');
0.0.0.0
是绑定到所有接口的特殊地址 – 例如:如果您在0.0.0.0:8000
上0.0.0.0:8000
您可以接受本地主机,局域网中的计算机或公共计算机发出的请求(如果您的计算机具有公共IP地址)。