【问题标题】:node.js with http and https routing带有 http 和 https 路由的 node.js
【发布时间】:2015-05-24 14:11:56
【问题描述】:

目前我让我的 node.js 项目同时创建 http 和 https 服务器。

var httpServer = http.createServer(app);
httpServer.listen(3000);

var httpsOptions = {
  ca: fs.readFileSync(config.https.ssl.ca, 'utf8'),
  key: fs.readFileSync(config.https.ssl.key, 'utf8'),
  cert: fs.readFileSync(config.https.ssl.cert, 'utf8')
};
var httpsServer = https.createServer(httpsOptions, app);
httpsServer.listen(8000);

我还使用此中间件将所有 http 流量重定向到 https。

app.all('*', function(req, res, next){  
  var host = req.header("host");

  if (host && host.indexOf('localhost') !== -1) {
    next()
  } else if (req.connection.encrypted) {
    next();
  } else {
    res.redirect('https://' + host + req.url);
  }
});

但对于某些页面我不需要 https 连接,例如 http://www.domain.com/shops/ 路由。我可以让这条路线使用http方法,而所有其他路线仍然使用https吗?

p.s:此页面从其他路由请求资源,如 bower_components、public 等。

【问题讨论】:

    标签: node.js http ssl https


    【解决方案1】:

    您可以找到如下请求路径,这可能会帮助您相应地重定向您的请求。

    var url_parts = url.parse(req.url);
    console.log(url_parts);
    console.log(url_parts.pathname);
    

    【讨论】:

      猜你喜欢
      • 2013-03-04
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      相关资源
      最近更新 更多