https サービス インターフェイスをすばやく構成するための小さなプログラム - プロジェクトはローカル https サービスを構成するための NodeJS です。ソース コードをダウンロードする必要がなく、支払いも必要ありません。Git の openssh 機能を直接使用します (プロテスト効果的)。

バックグラウンド

教師のノード JS をバックエンド サーバーとして使用する Netease クラウド ミュージック アプレットの開発を学習するには、オンライン アプレットの試用版には https インターフェイスが必要です。

次のステップは、NodeJs サービス https を構成することで発見された落とし穴と意外な問題を構成することです。

===== フォローアップ:

アプレットの https サービスは IP アドレスでは開始できません。次のプロセスを設定しても無駄です。それでも 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 -outcertificate.pem

この時点で、プロジェクトフォルダーに 3 つのキーファイルが生成されます。

上記で生成された 3 つのキー ファイルをノードプロジェクトにコピーし、起動スクリプトを作成します

 nodeJS バックエンド サービスを開始します。

Openss が認識できない問題が発生しました

1. 「Openssl」が内部コマンドまたは外部コマンドとして認識されない

その理由は、OpenSSL が Windows システムにインストールされていないためです。これはシステムに組み込まれていないため、自分でダウンロードしてインストールする必要があります。

ソース URL  /source/index.html

ダウンロード: このダウンロードは比較されます。ゆっくりお待ちください

       1) Web サイト: Windows 用 Win32/Win64 OpenSSL インストーラー - Shining Light Productions、画像をクリックして完全版を選択します。

 または、Baidu Netdisk を使用して以下をダウンロードします。

リンク: https://pan.baidu.com/s/1PRw1rEDP853KkujUJdaAKA?pwd=8qz0 
抽出コード: 8qz0

ダウンロードが完了したら、インストールします。

 インストールが完了すると、10元の報酬が必要となり、1回限りの設定が完了します。にもかかわらず。

管理者はまずインストール状況を確認します。表示はまだ不明です

お金を集めたい、諦めます~~~

構成キーをすばやく認識します (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 -outcertificate.pem

最後に次を実行します。

openssl x509 -req -in certrequest.csr -signkey privatekey.pem -outcertificate.pem 

3 つのファイルを生成し、プロジェクトのルート ディレクトリの下の新しいキー ディレクトリにコピーします。

 証明書リクエスト.csr

秘密鍵.pem

証明書.pem

 次に、サービス起動スクリプトを作成します。

http と https はソース コードを同時に提供します。プロジェクトのルート ディレクトリに httpsAndHttp.js を作成し、次のコードをそこにコピーします。

var SSLPORT = 499; //https port 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 に変更されます。次に、ウェブストームのターミナル コマンド ウィンドウで、次を実行します。

ノードhttpsAndHttp.js

 起動は成功しました。検証を開きます: OK--- 満足です

Webページを開き、次のように入力します 

https://ローカルホスト:499/

 プロンプトは安全ではありませんが、心配する必要はありません。[詳細設定] をクリックすると、アクセスを続行するボタンがあります。

この方法が正しいことを確認してから、NetEase Cloud オープンソースのバックエンド サーバーを構成します。

Netease Cloud Music バックエンド https を構成する

次に、研究中のアプレットのバックエンド サービス、ソース コード Github オープン ソース プロジェクトを構成します。使用: NetEase Cloud Music NodeJS バージョン API

 バックエンド インターフェイスの使用に関するリファレンス:

Shang シリコンバレー WeChat ミニ プログラム開発は NetEase Cloud 音楽アプリを模倣 ミニ プログラム バックエンド インターフェイス サーバー構築_Lan.W のブログ-CSDN ブログ

元のプロジェクトのserver.jsを変更し、主にインポートする3つのキーファイルを追加し、監視をhttps監視に変更します。

var privateKey = fs.readFileSync('./key/privatekey.pem', 'utf8') //キーのパス、エンコーディング
varcertificate = fs.readFileSync('./key/certificate.pem', 'utf8')
var credentials = {key: privateKey, cert: 証明書}
// 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}`) 
}) 
//lani 
var https = require('https'); で追加します。
var httpsServer = https.createServer(資格情報, アプリ); 
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 プロトコルに変更します。再度実行すると、データは完璧になります。

アプレットはドメイン名を設定します。

解決策がありません。 

 参考:ノード構成ローカル https service_cainiaoyizhan 2020 ブログ - CSDN ブログ

おすすめ

転載: blog.csdn.net/LlanyW/article/details/131360006