【问题标题】:HTTPS server not working on Android mobile phone but HTTP does?HTTPS 服务器不能在 Android 手机上运行,​​但 HTTP 可以?
【发布时间】:2017-05-14 12:57:59
【问题描述】:

我目前正在学习如何使用 Node js 和其他 Javascript api 创建服务器,并且我已经构建了一个 HTTP 和 HTTPS 服务器,它在我的 nat 网络的每个 pc navigator 上都可以正常工作,但是当我尝试使用我的 https 服务器时android 移动设备:https://192.168.1.20:8443,这是我构建 node.js 服务器的地方

我也想知道我是否可以让我的服务器可见或使用我的路由器公共 IP 地址访问它,我该怎么做。

这是我的服务器代码:

var fs = require('fs');
var http = require('http');
var https = require('https');
var cors = require('cors');
var privateKey  = fs.readFileSync('host.key', 'utf8');
var certificate = fs.readFileSync('host.cert', 'utf8');

var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();
app.use(cors());
app.use('/work', express.static('/.'))
app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
    console.log('Servidor en marxa.');
});


var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(8080);
httpsServer.listen(8443);

【问题讨论】:

    标签: javascript android node.js express


    【解决方案1】:

    几个小时后,我才意识到我需要将路由器端口转发到我构建 Node.js 服务器的位置。我必须同时转发 HTTP (#80) 和 HTTPS (#443) 才能通过公共 IP 地址授予访问权限。

    还必须修改我的部分 Javascript 服务器代码。

    var fs = require('fs');
    var http = require('http');
    var https = require('https');
    var cors = require('cors');
    var privateKey  = fs.readFileSync('host.key', 'utf8');
    var certificate = fs.readFileSync('host.cert', 'utf8');
    
    var credentials = {key: privateKey, cert: certificate};
    var express = require('express');
    var app = express();
    app.use(cors());
    var path = require('path');
    app.use(express.static(path.join(__dirname, 'public')));
    app.get('/', function(req, res){
        res.sendFile(__dirname + '/index.html');
        console.log('Servidor en marxa.');
    });
    
    // your express configuration here
    
    var httpServer = http.createServer(app);
    var httpsServer = https.createServer(credentials, app);
    
    httpServer.listen(80,"0.0.0.0");
    httpsServer.listen(443,"0.0.0.0");
    var io = require("socket.io")(httpServer);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多