服务器:
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:8080进行呼叫时工作正常,但是我尝试通过输入我的IP和端口(XXX.XXX.XXX)连接到我的手机(不在此网络连接,而是4G) .XXX:8080)
为什么这不起作用,我做错了什么?
答案 0 :(得分:1)
使用:
http.createServer(onRequest).listen(8080, "0.0.0.0")
因为否则你只能监听localhost接口。
最好将Context-Type
替换为Content-Type
。
答案 1 :(得分:0)
从另一个问题回答[重复]:
请勿忘记通过路由器设置转发您的端口,因为路由器正在接收请求而不是您的PC