首先最重要的是 你要明白 https接口的接收或者发送 的形式 是  https://域名:端口号  
而不是 https://ip:端口号
 
一,首先,去阿里云注册免费ssl证书
 
1,在搜索框中输入关键字证书,点击SSL证书
 

2,点击购买证书

3,选择免费型,点击购买

4,点击支付

 5,点击确认支付

6,支付成功后进入证书控制台

 7,点击申请

8,填写信息,点击下一步,最后点击验证,等待审核结束。

9,证书下来之后进入阿里云证书管理控制台下载证书  (nginx),如图

 10,下载解压后文件中包含 .key和 .pem 两个文件。 (a.key是为了方便自定义的名称)

11,将下载解压后包含.key和 .pem 两个文件的文件夹上传到自己的项目中。

 

12,将以下代码复制粘贴到www文件中,https端口根据需求自己修改 

#!/usr/bin/env node
/**
 * Module dependencies.
 */
var app = require('../app');
var debug = require('debug')('demo:server');
var http = require('http');
var fs = require('fs')
var https = require('https')
var path = require('path')
var express = require('express')
const options = {
  key: fs.readFileSync(path.join(__dirname, '../cert/a.key')),
  cert: fs.readFileSync(path.join(__dirname, '../cert/a.pem'))
}
app.set('porthttps',11443)  
/**
 * Get port from environment and store in Express.
 */
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
 * Create HTTP server.
 */
var server = http.createServer(app);
/**
 * Listen on provided port, on all network interfaces.
 */
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
https.createServer(options,app).listen(app.get('porthttps'),function(){
  console.log('11443端口运行');
})
/**
 * Normalize a port into a number, string, or false.
 */
function normalizePort(val) {
  var port = parseInt(val, 10);
  if (isNaN(port)) {
    // named pipe
    return val;
  }
  if (port >= 0) {
    // port number
    return port;
  }
  return false;
}
/**
 * Event listener for HTTP server "error" event.
 */
function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }
  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;
  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}
/**
 * Event listener for HTTP server "listening" event.
 */
function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

由衷感谢帮助我解决此问题的夏初秋博主  https://www.cnblogs.com/xiaqiuchu/

express 配置 https 服务 ( 以阿里云服务器为例), 探索一周终于搞定的更多相关文章

  1. Nginx 配置 HTTPS 完整过程(阿里云申请免费版一年ssl证书)

    1. nginx 的 ssl 模块安装 查看 nginx 是否安装 http_ssl_module 模块. $ /usr/local/nginx/sbin/nginx -V 如果出现 configur ...

  2. 阿里云服务器Linux CentOS安装配置(零)目录

    阿里云服务器Linux CentOS安装配置(零)目录 阿里云服务器Linux CentOS安装配置(一)购买阿里云服务器 阿里云服务器Linux CentOS安装配置(二)yum安装svn 阿里云服 ...

  3. 阿里云服务器发送邮件:Connection could not be established with host smtp.qq.com [Connection timed out #110]

    阿里云服务器发送邮件:Connection could not be established with host smtp.qq.com [Connection timed out #110] 一.总 ...

  4. 记录第一次阿里云服务器部署java web工程的经历

    起因:测试一个微信小程序,发现所有的请求要求为https的形式,开发工具忽略后手机无法测试,故尝试配置. 准备:阿里云服务器一台 域名一个(解析在服务器)     tomcat7.0.54     j ...

  5. 阿里云服务器linux主机如何添加swap分区

    为什么要添加Swap分区?swap分区,即交换区,作用为:当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作 ...

  6. 阿里云服务器 centos7 中继邮箱+转发服务 详细配置

    阿里云centos7 邮箱转发服务配置 一.文档编写目的: 网络拓扑图: 阿里云服务器屏蔽掉25端口后,内网服务器如何通过跳板机发送邮件到外网邮箱. 如果是可联网的阿里云机器,可以直接配置mailx使 ...

  7. 阿里云服务器CentOS6.9 tomcat配置https安全访问

    应用场景 上线微信小程序的时候,域名要求https安全格式,否则获取数据异常. 第一步.SSL证书获取 获取SSL证书方式很多种,包括网页生成.工具生成等,这里我使用阿里云平台获取免费ssl证书的方法 ...

  8. 阿里云服务器下安装LAMP环境(CentOS Linux 6.3) 安装与配置 Apache 服务

    想让我们的阿里云服务器成为一台 Web 服务器,我们需要安装一个 Web 服务器软件,比如 Apache ,或者 Nginx 等等.下面我们就一起来安装一个 Apache 服务. 我们可以使用 yum ...

  9. 阿里云服务器 配置 tomcat 发布spring boot项目 的具体操作 【使用公网ip】

    1.前言 spring boot 转成war包 后用tomcat发布的具体操作在我另一篇随笔有详细记载,不论是window系统还是Linux系统,tomcat的发布配置都是一样的,所以这里不具体讲这个 ...

随机推荐

  1. centos 默认php 版本太低移到高版本的办法

    // centos 默认有php 版本太低转移到高版本的解决办法 php -v 版本低 ln -s /usr/local/php/bin/php /usr/bin/php

  2. Python - 编程技巧,语法糖,黑魔法,pythonic

    参考,搬运 http://python-web-guide.readthedocs.io/zh/latest/idiom/idiom.html 待定 1. Python支持链式比较 # bad a = ...

  3. Web--Utils

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. 学习不一样的vue4:mock与axios实战1

    学习不一样的vue4:mock与axios实战1  发表于 2017-06-14 |  分类于 web前端|  |  阅读次数 8180 首先 首发博客: 我的博客 项目源码: 源码(喜欢请star) ...

  5. Git远程分支代码强制回退&Tag添加

    Git指令大全:https://www.alexkras.com/getting-started-with-git/ Git提交错了,还是Master分支,哎呦喂咋整?请见下文.   [场景描述] 项 ...

  6. rem与部分手机 字体偏大问题

    原因是部分手机自己设置了巨无霸字体.

  7. 重识线段树——Let's start with the start.

    声明 本文为 Clouder 原创,在未经许可情况下请不要随意转载.原文链接 前言 一般地,这篇文章是给学习过线段树却仍不透彻者撰写的,因此在某些简单的操作上可能会一笔带过. 当然了,入门线段树后也可 ...

  8. ng-repeat 设定select 选择项

    <select class="form-control m-b" name="FPermissionID" ng-model="mgfunc.F ...

  9. luogu P2754 [CTSC1999]家园

    本题是分层图最大流问题,相当于按时间拆点,每个当前点向下一点的下一时间层连点,每一层有n+1个点 #include<bits/stdc++.h> using namespace std; ...

  10. Golang gin框架学习

    今天开始学习gin框架,在Github上找的示例的go-gin-example, 进度 日期 进展 疑惑 进展 1.30 下拉代码,初步了解gin的介绍.搭建 .mod文件 module原理.使用方法 ...