不能从外部连接到我的Node.Js服务器? (重复)
服务器:
var http = require('http') var fs = require('fs') //404 function _404(response) { response.writeHead(404, {'Context-Type': 'text/plain'}) response.write('error 404, not found') response.end() } http.createServer(onRequest).listen(8080) console.log('server is running') function onRequest(request, response) { if (request.method == 'GET' && request.url == '/') { response.writeHead(200, {'Context-Type': 'text/html'}) fs.createReadStream('./testhtml.html').pipe(response) } else { _404(response) } }
当我进入我的浏览器,并调用本地主机:8080,但我试图通过input我的IP和端口(XXX.XXX.XXX.XXX:我的手机(不是在这个networking连接,但4G) 8080)
为什么这不起作用,我做错了什么?
使用:
http.createServer(onRequest).listen(8080, "0.0.0.0")
因为否则你只能听localhost接口。
也可以用Content-Type
replaceContext-Type
Content-Type
。
从另一个问题的答案[重复]:
不要忘记通过路由器设置转发您的端口,导致路由器收到请求,而不是您的电脑