Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从外部连接到我的Node.Js服务器?(副本)_Node.js_Server - Fatal编程技术网

无法从外部连接到我的Node.Js服务器?(副本)

无法从外部连接到我的Node.Js服务器?(副本),node.js,server,Node.js,Server,服务器: 当我进入浏览器并使用localhost:8080呼叫时,工作正常,但我试图通过输入我的ip和端口(XXX.XXX.XXX.XXX:8080)用我的手机(不是在这个网络连接上,而是4G)连接到浏览器 为什么这不起作用,我做错了什么吗?使用: var http = require('http') var fs = require('fs') //404 function _404(response) { response.writeHead(404, {'Context-Type

服务器:

当我进入浏览器并使用localhost:8080呼叫时,工作正常,但我试图通过输入我的ip和端口(XXX.XXX.XXX.XXX:8080)用我的手机(不是在这个网络连接上,而是4G)连接到浏览器

为什么这不起作用,我做错了什么吗?

使用:

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)      
    }
}
因为否则您只能侦听localhost接口


另外,最好将
上下文类型
替换为
内容类型

另一个问题的答案[重复]:

不要忘记通过路由器设置转发端口,因为路由器接收的是请求,而不是你的电脑

http.createServer(onRequest).listen(8080, "0.0.0.0")