小程序 快速配置https服务接口 - 项目是nodeJS配置本地https服务,不用下载源码不用付费,直接使用Git的openssh功能(亲测有效)

news2025/5/10 21:47:02

背景

学习网易云音乐小程序开发,用了老师的node JS做后端服务器,上线小程序体验版必须要https接口。

接下来就是配置NodeJs服务https踩的坑跟发现的惊喜。

配置https

下载与配置

密钥生成

1 运行命令 : openssl genrsa -out privatekey.pem 1024

2 运行命令 : openssl req -new -key privatekey.pem -out certrequest.csr

3 运行命令 :openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem

到此,在项目文件夹里面生成三个密钥文件

把上面生成的三个密钥文件复制到node项目,创建启动脚本

 启动nodeJS 后端服务:

遇到问题Openss不认识

1. 'Openssl' is not recognized as an internal or external command

原因是未在windows系统安装Openssl,这个不是系统自带,需要自己去下载安装

源码网址 /source/index.html

下载: 这个下载比较,慢慢等一下

       1)网站:Win32/Win64 OpenSSL Installer for Windows - Shining Light Productions,点击如 图选择完整版;

 或者用百度网盘下载:

链接:https://pan.baidu.com/s/1PRw1rEDP853KkujUJdaAKA?pwd=8qz0 
提取码:8qz0

下载完成之后安装:;

 安装完成之后要求打赏10元,一次性配置完成。不管。

先管理员看看安装情况 :显示还是未认识

要收钱,弃~~~

快捷实现配置密钥(已经安装git情况下)惊喜!!!!!

无解了,然后搜索了一下电脑发现很多地方都有openSSh这些东西,

之前用git配置ssh密钥,想着能不能用这个git软件现在实现,接着随便找了个目录,右键打开  git控制窗口

然后在git bath批处理窗口 ,执行3个生成密钥文件命令:

1 运行命令 : openssl genrsa -out privatekey.pem 1024

2 运行命令 : openssl req -new -key privatekey.pem -out certrequest.csr

3 运行命令 :openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem

最后执行:

openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem 

生成3个文件,复制到项目根目录下的一个新建key的目录

 certrequest.csr

privatekey.pem

certificate.pem

 然后创建服务启动脚本 :

http与https同时服务源码:在项目根目录下创建: httpsAndHttp.js, 然后把下面的代码复制进去,

var SSLPORT = 499; //https 端口 当 https 使用默认端口的时候可能会出现如下报错,把默认443修改成其它不被占用的端口就可以正常访问。
var app = require('express')();
var fs = require('fs');
var http = require('http');
var https = require('https');

var privateKey  = fs.readFileSync('./key/privatekey.pem', 'utf8'); //密钥路径,编码
var certificate = fs.readFileSync('./key/certificate.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
var PORT = 3030; //http 端口
// var SSLPORT = 443; //https 端口
var SSLPORT = 499; //https 端口

httpServer.listen(PORT, function() {
  console.log('HTTP Server is running on: http://localhost:%s', PORT);
});

httpsServer.listen(SSLPORT, function() {
  console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT);
});

// Welcome
app.get('/', function(req, res) {
  if(req.protocol === 'https') {
    res.status(200).send('Welcome https!');
  }
  else {
    res.status(200).send('Welcome http!');
  }
});

 加密不用443,改成了499端口,防止冲突。然后,在webstorm里的终端指令窗口,执行

node httpsAndHttp.js

 启动成功,打开验证:OK--- 开心

打开网页,输入 

https://localhost:499/

 提示不安全,不用管,点击高级,里面有个继续访问的按钮 。

验证此方法ok了,接下来配置网易云开源的后端服务器

配置网易云音乐后端https

现在配置正在学习小程序的后端服务,源码Github开源项目:使用:网易云音乐 NodeJS 版 API

 后端接口使用参考:

尚硅谷微信小程序开发 仿网易云音乐App 小程序 后端接口服务器搭建_Lan.W的博客-CSDN博客

修改原项目的server.js,主要是添加3个密钥文件引入,监听改成https的监听

var privateKey = fs.readFileSync('./key/privatekey.pem', 'utf8') //密钥路径,编码
var certificate = fs.readFileSync('./key/certificate.pem', 'utf8')
var credentials = {key: privateKey, cert: certificate}
// var SSLPORT = 443; //https 端口
var SSLPORT = 488 //https 端口 当 https 使用默认端口的时候可能会出现如下报错,把默认443修改成其它不被占用的端口就可以正常访问。

....//监听的地址,配置https

const appExt = app
// httpServer.listen
console.log(">> 设置客户端请求监听",port,host)
appExt.server = app.listen(port, host, () => {
  console.log(`server running @ http://${host ? host : 'localhost'}:${port}`)
})
//add by lani
var https = require('https');
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(SSLPORT, host, () => {
  console.log(`server running @ https://${host ? host : 'localhost'}:${SSLPORT}`)
})

 server.js修改之后整个文件代码

const fs = require('fs')
const path = require('path')
const express = require('express')
const request = require('./util/request')
const packageJSON = require('./package.json')
const exec = require('child_process').exec
const cache = require('./util/apicache').middleware
const {cookieToJson} = require('./util/index')
const fileUpload = require('express-fileupload')
const decode = require('safe-decode-uri-component')
// add by lani 添加3个密钥文件
var privateKey = fs.readFileSync('./key/privatekey.pem', 'utf8') //密钥路径,编码
var certificate = fs.readFileSync('./key/certificate.pem', 'utf8')
var credentials = {key: privateKey, cert: certificate}
// var SSLPORT = 443; //https 端口
var SSLPORT = 488 //https 端口 当 https 使用默认端口的时候可能会出现如下报错,把默认443修改成其它不被占用的端口就可以正常访问。


/**
 * The version check result.
 * @readonly
 * @enum {number}
 */
const VERSION_CHECK_RESULT = {
  FAILED: -1,
  NOT_LATEST: 0,
  LATEST: 1,
}

/**
 * @typedef {{
 *   identifier?: string,
 *   route: string,
 *   module: any
 * }} ModuleDefinition
 */

/**
 * @typedef {{
 *   port?: number,
 *   host?: string,
 *   checkVersion?: boolean,
 *   moduleDefs?: ModuleDefinition[]
 * }} NcmApiOptions
 */

/**
 * @typedef {{
 *   status: VERSION_CHECK_RESULT,
 *   ourVersion?: string,
 *   npmVersion?: string,
 * }} VersionCheckResult
 */

/**
 * @typedef {{
 *  server?: import('http').Server,
 * }} ExpressExtension
 */

/**
 * Get the module definitions dynamically.
 *
 * @param {string} modulesPath The path to modules (JS).
 * @param {Record<string, string>} [specificRoute] The specific route of specific modules.
 * @param {boolean} [doRequire] If true, require() the module directly.
 * Otherwise, print out the module path. Default to true.
 * @returns {Promise<ModuleDefinition[]>} The module definitions.
 *
 * @example getModuleDefinitions("./module", {"album_new.js": "/album/create"})
 */
async function getModulesDefinitions(
  modulesPath,
  specificRoute,
  doRequire = true,
) {
  const files = await fs.promises.readdir(modulesPath)
  const parseRoute = (/** @type {string} */ fileName) =>
    specificRoute && fileName in specificRoute
      ? specificRoute[fileName]
      : `/${fileName.replace(/\.js$/i, '').replace(/_/g, '/')}`

  const modules = files
    .reverse()
    .filter((file) => file.endsWith('.js'))
    .map((file) => {
      const identifier = file.split('.').shift()
      const route = parseRoute(file)
      const modulePath = path.join(modulesPath, file)
      const module = doRequire ? require(modulePath) : modulePath

      return {identifier, route, module}
    })

  return modules
}

/**
 * Check if the version of this API is latest.
 *
 * @returns {Promise<VersionCheckResult>} If true, this API is up-to-date;
 * otherwise, this API should be upgraded and you would
 * need to notify users to upgrade it manually.
 */
async function checkVersion() {
  return new Promise((resolve) => {
    exec('npm info NeteaseCloudMusicApi version', (err, stdout) => {
      if (!err) {
        let version = stdout.trim()

        /**
         * @param {VERSION_CHECK_RESULT} status
         */
        const resolveStatus = (status) =>
          resolve({
            status,
            ourVersion: packageJSON.version,
            npmVersion: version,
          })

        resolveStatus(
          packageJSON.version < version
            ? VERSION_CHECK_RESULT.NOT_LATEST
            : VERSION_CHECK_RESULT.LATEST,
        )
      }
    })

    resolve({
      status: VERSION_CHECK_RESULT.FAILED,
    })
  })
}

/**
 * Construct the server of NCM API.
 *
 * @param {ModuleDefinition[]} [moduleDefs] Customized module definitions [advanced]
 * @returns {Promise<import("express").Express>} The server instance.
 */
async function consturctServer(moduleDefs) {
  console.log(">>>开始创建请求配置")
  const app = express()  // express, 同一个app
  const {CORS_ALLOW_ORIGIN} = process.env
  app.set('trust proxy', true)

  /**
   * CORS & Preflight request
   */
  app.use((req, res, next) => {
    if (req.path !== '/' && !req.path.includes('.')) {
      res.set({
        'Access-Control-Allow-Credentials': true,
        'Access-Control-Allow-Origin':
          CORS_ALLOW_ORIGIN || req.headers.origin || '*',
        'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type',
        'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS',
        'Content-Type': 'application/json; charset=utf-8',
      })
    }
    req.method === 'OPTIONS' ? res.status(204).end() : next()
  })

  /**
   * Cookie Parser
   */
  app.use((req, _, next) => {
    req.cookies = {}
    //;(req.headers.cookie || '').split(/\s*;\s*/).forEach((pair) => { //  Polynomial regular expression //
    ;(req.headers.cookie || '').split(/;\s+|(?<!\s)\s+$/g).forEach((pair) => {
      let crack = pair.indexOf('=')
      if (crack < 1 || crack == pair.length - 1) return
      req.cookies[decode(pair.slice(0, crack)).trim()] = decode(
        pair.slice(crack + 1),
      ).trim()
    })
    next()
  })

  /**
   * Body Parser and File Upload
   */
  app.use(express.json())
  app.use(express.urlencoded({extended: false}))

  app.use(fileUpload())

  /**
   * Serving static files
   */
  app.use(express.static(path.join(__dirname, 'public')))

  /**
   * Cache
   */
  app.use(cache('2 minutes', (_, res) => res.statusCode === 200))

  /**
   * Special Routers
   */
  const special = {
    'daily_signin.js': '/daily_signin',
    'fm_trash.js': '/fm_trash',
    'personal_fm.js': '/personal_fm',
  }

  /**
   * Load every modules in this directory
   */
  const moduleDefinitions =
    moduleDefs ||
    (await getModulesDefinitions(path.join(__dirname, 'module'), special))

  for (const moduleDef of moduleDefinitions) {
    // Register the route.
    app.use(moduleDef.route, async (req, res) => {
      ;[req.query, req.body].forEach((item) => {
        if (typeof item.cookie === 'string') {
          item.cookie = cookieToJson(decode(item.cookie))
        }
      })

      let query = Object.assign(
        {},
        {cookie: req.cookies},
        req.query,
        req.body,
        req.files,
      )

      try {
        const moduleResponse = await moduleDef.module(query, (...params) => {
          // 参数注入客户端IP
          const obj = [...params]
          let ip = req.ip

          if (ip.substr(0, 7) == '::ffff:') {
            ip = ip.substr(7)
          }
          // console.log(ip)
          obj[3] = {
            ...obj[3],
            ip,
          }
          return request(...obj)
        })
        console.log('>> 收到请求, [OK]', decode(req.originalUrl))

        const cookies = moduleResponse.cookie
        if (!query.noCookie){
          if (Array.isArray(cookies) && cookies.length > 0) {
            if (req.protocol === 'https') {
              // Try to fix CORS SameSite Problem
              console.log(">>> run https")
              res.append(
                'Set-Cookie',
                cookies.map((cookie) => {
                  return cookie + '; SameSite=None; Secure'
                }),
              )
            } else {
              console.log(">>> run http")
              res.append('Set-Cookie', cookies)
            }
          }
        }
        res.status(moduleResponse.status).send(moduleResponse.body)
      } catch (/** @type {*} */ moduleResponse) {
        console.log('[ERR]', decode(req.originalUrl), {
          status: moduleResponse.status,
          body: moduleResponse.body,
        })
        if (!moduleResponse.body) {
          res.status(404).send({
            code: 404,
            data: null,
            msg: 'Not Found',
          })
          return
        }
        if (moduleResponse.body.code == '301')
          moduleResponse.body.msg = '需要登录'
        if (!query.noCookie) {
          res.append('Set-Cookie', moduleResponse.cookie)
        }

        res.status(moduleResponse.status).send(moduleResponse.body)
      }
    })
  }
  console.log(">>请求服务 初始化完成 。")
  return app
}

/**
 * Serve the NCM API.
 * @param {NcmApiOptions} options
 * @returns {Promise<import('express').Express & ExpressExtension>}
 */
async function serveNcmApi(options) {
  const port = Number(options.port || process.env.PORT || '3000')
  const host = options.host || process.env.HOST || ''

  const checkVersionSubmission =
    options.checkVersion &&
    checkVersion().then(({npmVersion, ourVersion, status}) => {
      if (status == VERSION_CHECK_RESULT.NOT_LATEST) {
        console.log(
          `最新版本: ${npmVersion}, 当前版本: ${ourVersion}, 请及时更新`,
        )
      }
    })
  const constructServerSubmission = consturctServer(options.moduleDefs)

  const [_, app] = await Promise.all([
    checkVersionSubmission,
    constructServerSubmission,
  ])

  /** @type {import('express').Express & ExpressExtension} */
  const appExt = app
  // httpServer.listen
  console.log(">> 设置客户端请求监听",port,host)
  appExt.server = app.listen(port, host, () => {
    console.log(`server running @ http://${host ? host : 'localhost'}:${port}`)
  })
  //add by lani
  var https = require('https');
  var httpsServer = https.createServer(credentials, app);
  httpsServer.listen(SSLPORT, host, () => {
    console.log(`server running @ https://${host ? host : 'localhost'}:${SSLPORT}`)
  })

  return appExt
}

module.exports = {
  serveNcmApi,
  getModulesDefinitions,
}
配置完成之后,打开cmd,进入当前项目目录执行服务启动指令:node app.js

 运行测试:

 接下就可以开心使用体验版小程序调用https接口了。

 打开postman,把上次用来测试推荐歌单,http改成https协议。再次执行,完美出数据了。

小程序设置域名:

ip无解了。 

 

 参考:node配置本地https服务_菜鸟驿站2020的博客-CSDN博客

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/679575.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Pyecharts】| 风电场运维可视化系统 | 文末送书

目录 一. &#x1f981; 摘要二. &#x1f981; 文章结构三. &#x1f981; 所用开发工具与相关技术3.1 PyCharm3.2 Flask3.3 Pyecharts3.4 Jinja2 四. &#x1f981; 设计与实现4.1 系统总体需求4.1.1 数据采集和处理4.1.2 可视化设计4.1.3 后台功能实现 4.2 系统用户模块的实…

「小产品独立变现实战训练营1期」正在进行中

本次训练营共3周&#xff0c;目前正在进行中的第2周&#xff0c;同学正在认真完成作业&#xff0c;跟随教学视频和产品源码进行学习。 大家好&#xff0c;我是凯凯刘&#xff0c;一名程序员&#xff0c;一直在做小产品的开发变现&#xff0c;目标就是让小产品的变现更简单。【小…

认识异常

目录 异常的概念与体系结构 异常的概念 异常的体系结构 异常的分类 异常的处理 防御式编程 1. LBYL: 2. EAFP: 异常的抛出 异常的捕获 异常声明throws try-catch捕获并处理 关于异常的处理方式 finally 异常的处理流程 自定义异常类 异常的概念与体系结构 异常…

【吃透网络安全】2023软考网络管理员考点网络安全(五)包过滤等多种防火墙详解

涉及知识点 什么是包过滤防火墙、应用层网关防火墙、状态监测防火墙&#xff1f;包过滤防火墙的概念及定义&#xff0c;包过滤防火墙、应用层网关防火墙的优缺点&#xff0c;软考网络管理员常考知识点&#xff0c;软考网络管理员网络安全&#xff0c;网络管理员考点汇总。 后…

深入理解深度学习——BERT派生模型:ALBERT(A Lite BERT)

分类目录&#xff1a;《深入理解深度学习》总目录 预训练语言模型的一个趋势是使用更大的模型配合更多的数据&#xff0c;以达到“大力出奇迹”的效果。随着模型规模的持续增大&#xff0c;单块GPU已经无法容纳整个预训练语言模型。为了解决这个问题&#xff0c;谷歌提出了ALBE…

Redis【web应用实践_网页缓存、其他功能(发布与订阅、慢查询 、流水线pipeline)】(四)-全面详解(学习总结---从入门到深化)

目录 ​编辑 Redis构建web应用实践_网页缓存 创建springboot项目 选择组件 编写配置文件 创建表 编写持久层 编写业务层 编写控制层 下载压测工具 启动Jmeter工具 修改语言 创建压测任务 添加HTTP请求 配置HTT请求 添加压测结果报告 没有加缓存的吞吐量 ​ …

【java爬虫】selenium+browsermob入门实战

在爬虫领域&#xff0c;selenium几乎是最无所不能的一个框架了&#xff0c;在传统的爬虫无能为力的时候&#xff0c;我们可以使用selenium来请求动态页面获取信息。 当然&#xff0c;只有selenium还是不够的&#xff0c;因为使用selenium我们只能获取页面上展示的数据&#xf…

Linux驱动入门(四)——内核进程管理

文章目录 前言进程进程描述符及任务结构分配进程描述符进程描述符的存放进程状态设置当前进程状态进程上下文进程家族树 进程创建写时拷贝fork()vfork() 线程在Linux中的实现创建线程内核线程 进程终结删除进程描述符孤儿进程造成的进退维谷 总结 前言 进程是Unix操作系统抽象…

Vue--》Vue3打造可扩展的项目管理系统后台的完整指南(七)

今天开始使用 vue3 ts 搭建一个项目管理的后台&#xff0c;因为文章会将项目的每一个地方代码的书写都会讲解到&#xff0c;所以本项目会分成好几篇文章进行讲解&#xff0c;我会在最后一篇文章中会将项目代码开源到我的GithHub上&#xff0c;大家可以自行去进行下载运行&…

驱动开发:摘除InlineHook内核钩子

在笔者上一篇文章《驱动开发&#xff1a;内核层InlineHook挂钩函数》中介绍了通过替换函数头部代码的方式实现Hook挂钩&#xff0c;对于ARK工具来说实现扫描与摘除InlineHook钩子也是最基本的功能&#xff0c;此类功能的实现一般可在应用层进行&#xff0c;而驱动层只需要保留一…

idea如何使用git指令

&#xff08;1&#xff09;、打开setting,直接搜git (2)、点一下text 如果显示成功就不用管了&#xff0c;但如果失败就要重新设置一下目录&#xff1a; &#xff08;3&#xff09;、找到自己设置gitee ssh目录&#xff1a; 这里我们是不需要用.git对.idea进行管理的&#xff…

vue3原理和源码分析 - VirtualDOM和DOMDIFF

目录 VUE3的h/createVNode函数【vue的概念模型】 Virtual DOM&#xff08;组件化的概念模型&#xff09; VirtualDOM更新 WHY DOM-DIFF&#xff1f; DOM-DIFF原理 DOM-DIFF伪代码 DOM-DIFF分类讨论&#xff1a;属性变更 DOM-DIFF分类讨论&#xff1a;节点类型不同 DOM-…

正负整数小数在内存的存储

目录 补码引入 整数在内存的存储 小数十进制与二进制相互转化 小数的内存存储 本节会主要学习正负的整数小数在内存的存储方式 补码引入 补码在计算机有着重要的地位&#xff0c;计算机本身只能完成的加法&#xff0c;移位运算&#xff0c;减法&#xff0c;乘法&#xff0…

深入解析多人共享云盘:便捷文件协作与分享的全新模式

在当今数字化时代&#xff0c;云盘已成为许多人存储和共享文件的首选方式。但是&#xff0c;传统的个人云盘服务在多人协作方面存在一些限制。为了解决这个问题&#xff0c;多人共享云盘应运而生。什么是多人共享云盘&#xff1f; 多人共享云盘是一种允许多个用户同时访问、编辑…

【数据库六】存储过程

存储过程 1.存储过程概述1.1 存储过程定义1.2 存储过程优点1.3 创建存储过程 2. 存储过程参数2.1 输入参数2.2 输出参数2.3 输入输出参数2.4 存储过程参数总结 3. 删除存储过程4.存储过程的控制语句4.1 条件语句if --else4.2 循环语句4.3 存储过程控制语句总结 1.存储过程概述 …

Qt 实现SQLite全部语法(增删改查、内置函数、高级语法)

Qt 实现SQLite全部语法 【1】SQLite Qt界面设计【2】SQLite Qt数据库创建、打开、关闭、删除【3】SQLite Qt表格的创建【4】SQLite Qt表格的插入【5】SQLite Qt表格的查询【6】SQLite Qt表格的删除【7】SQLite Qt表格的更新【8】SQLite Qt表格的结构【9】SQLite Qt表格的修改【…

Python 教程:从零到大师

首先, 什么是Python? 用python作者Guido van Rossum自己的话来说&#xff0c;Python是这样的一门语言&#xff1a; "它是一门高级编程语言, 它的核心设计理念是让所有代码变得更易阅读&#xff0c;并给开发者们提供一种“仅仅几行代码就能编写编程逻辑”的语法。 那么&am…

SD/StableDiffusion部署图文教程,ai绘画教程,实现谷歌云端零成本部署,中文UI

目录 一、前言 二、准备前提 三、教程说明 四、开始搭建 1、第一步&#xff0c;下载ipynb脚本文件 2、第二步&#xff0c;上传一键脚本文件到谷歌云盘 3、选择该.ipynb文件--右键--打开方式--关联更多应用 4、输入框搜索Colaboratory找到该应用&#xff0c;安装 5、安…

【瑞萨RA6系列】RASC+Keil开发环境搭建和GPIO点灯指南

瑞萨RASCKeil开发环境搭建 一、简单开箱二、资料下载三、芯片简介四、开发环境搭建4.1 安装RASC4.2 安装Keil MDK4.3 安装RA6E1的MDK支持包 五、GPIO点灯指南5.1 创建RASC项目5.2 查阅开发板原理图5.3 设置LED1引脚为输出3.4 编写LED1闪烁的代码5.5 编译Keil项目5.6 修改Keil调…

产品设计.B端设计师不可忽视的产品和用户

B端产品与C端产品不同&#xff0c;前者强调客户价值&#xff0c;企业决策链路长&#xff0c;用户难获得的同时也相对难流失。而作为B端产品设计师&#xff0c;就需要根据B端产品业务特点&#xff0c;从用户、产品等角度进行考量&#xff0c;以求做出符合市场和用户的设计方案。…