美文网首页
nodejs使用https请求

nodejs使用https请求

作者: 陆遥远 | 来源:发表于2017-11-20 09:21 被阅读0次
    var fs = require("fs");
    var express = require("express");
    
    var http = express.createServer();
    
    var httpsOptions = {
      key: fs.readFileSync('key.pem'),
      cert: fs.readFileSync('cert.pem')
    };
    
    var https = express.createServer(httpsOptions);
    
    http.all('*', function(req, res) {
      console.log("HTTP: " + req.url);
      return res.redirect("https://" + req.headers["host"] + req.url);
    });
    
    http.error(function(error, req, res, next) {
      return console.log("HTTP error " + error + ", " + req.url);
    });
    
    https.error(function(error, req, res, next) {
      return console.log("HTTPS error " + error + ", " + req.url);
    });
    
    https.all('*', function(req, res) {
      console.log("HTTPS: " + req.url);
      return res.send("Hello, World!");
    });
    
    http.listen(80);
    

    参考地址

    相关文章

      网友评论

          本文标题:nodejs使用https请求

          本文链接:https://www.haomeiwen.com/subject/gqbmvxtx.html