Node启动https服务器的教程

 更新时间:2018年03月18日 14:18:43   作者:任乃千  
这篇文章主要介绍了Node启动https服务器的教程,有node原生版本,express 版本,koa版本,具体各个版本的代码讲解大家参考下本文

首先你需要生成https证书,可以去付费的网站购买或者找一些免费的网站,可能会是key或者crt或者pem结尾的。不同格式之间可以通过OpenSSL转换,如:

openssl x509 -in mycert.crt -out mycert.pem -outform PEM

Node原生版本:

const https = require('https')
const path = require('path')
const fs = require('fs')
// 根据项目的路径导入生成的证书文件
const privateKey = fs.readFileSync(path.join(__dirname, './certificate/private.key'), 'utf8')
const certificate = fs.readFileSync(path.join(__dirname, './certificate/certificate.crt'), 'utf8')
const credentials = {
 key: privateKey,
 cert: certificate,
}
// 创建https服务器实例
const httpsServer = https.createServer(credentials, async (req, res) => {
 res.writeHead(200)
 res.end('Hello World!')
})
// 设置https的访问端口号
const SSLPORT = 443
// 启动服务器,监听对应的端口
httpsServer.listen(SSLPORT, () => {
 console.log(`HTTPS Server is running on: https://localhost:${SSLPORT}`)
})

express版本

const express = require('express')
const path = require('path')
const fs = require('fs')
const https = require('https')
// 根据项目的路径导入生成的证书文件
const privateKey = fs.readFileSync(path.join(__dirname, './certificate/private.key'), 'utf8')
const certificate = fs.readFileSync(path.join(__dirname, './certificate/certificate.crt'), 'utf8')
const credentials = {
 key: privateKey,
 cert: certificate,
}
// 创建express实例
const app = express()
// 处理请求
app.get('/', async (req, res) => {
 res.status(200).send('Hello World!')
})
// 创建https服务器实例
const httpsServer = https.createServer(credentials, app)
// 设置https的访问端口号
const SSLPORT = 443
// 启动服务器,监听对应的端口
httpsServer.listen(SSLPORT, () => {
 console.log(`HTTPS Server is running on: https://localhost:${SSLPORT}`)
})

koa版本

const koa = require('koa')
const path = require('path')
const fs = require('fs')
const https = require('https')
// 根据项目的路径导入生成的证书文件
const privateKey = fs.readFileSync(path.join(__dirname, './certificate/private.key'), 'utf8')
const certificate = fs.readFileSync(path.join(__dirname, './certificate/certificate.crt'), 'utf8')
const credentials = {
 key: privateKey,
 cert: certificate,
}
// 创建koa实例
const app = koa()
// 处理请求
app.use(async ctx => {
 ctx.body = 'Hello World!'
})
// 创建https服务器实例
const httpsServer = https.createServer(credentials, app.callback())
// 设置https的访问端口号
const SSLPORT = 443
// 启动服务器,监听对应的端口
httpsServer.listen(SSLPORT, () => {
 console.log(`HTTPS Server is running on: https://localhost:${SSLPORT}`)
})

总结

以上所述是小编给大家介绍的Node启动https服务器的教程,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

  • webpack启动服务器和处理sourcemap的操作方法

    webpack启动服务器和处理sourcemap的操作方法

    Source Map源代码地图就是解决此类问题最好的办法,从它的名字就能够看出它的作用:映射转换后的代码与源代码之间的关系,这篇文章主要介绍了webpack启动服务器和处理sourcemap的操作方法,需要的朋友可以参考下
    2024-03-03
  • Mac OSX下使用MAMP安装配置PHP开发环境

    Mac OSX下使用MAMP安装配置PHP开发环境

    本部分描述如何在 Mac 上安装 MAMP。将通过一个操作安装 Apache Web 服务器、MySQL 和phpMyAdmin,需要的朋友可以参考下
    2017-09-09
  • 安装Nacos服务器的详细过程

    安装Nacos服务器的详细过程

    Nacos是Dynamic Naming and Configuration Service的首字母简称,一个由阿里开发,用于云原始应用动态服务发现、配置管理和服务管理平台,这篇文章主要介绍了安装Nacos服务器的详细过程,需要的朋友可以参考下
    2024-03-03
  • Flink 侧流输出源码示例解析

    Flink 侧流输出源码示例解析

    这篇文章主要为大家介绍了Flink 侧流输出源码示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • 集群运维自动化工具ansible之使用playbook安装zabbix客户端

    集群运维自动化工具ansible之使用playbook安装zabbix客户端

    Zabbix客户端的安装配置:Zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。zabbix能监视各种网络参数,保证服务器系统的安全运营;本文讲述的是使用playbook安装zabbix客户端。
    2014-07-07
  • TortoiseSvn小乌龟安装最新图文详细教程

    TortoiseSvn小乌龟安装最新图文详细教程

    在使用TortoiseSvn安装过程中经常出现各种奇葩问题,干脆换成svn吧,在这里小编把我的安装过程记录下来,有需要的朋友直接拿去用吧
    2021-05-05
  • ubuntu20.04安装unity-tweak-tools启动时遇到错误的解决

    ubuntu20.04安装unity-tweak-tools启动时遇到错误的解决

    在Ubuntu系统中,安装Unity Tweak Tool时可能会遇到schemacom.canonical.Unity.ApplicationsLens未安装的错误,解决这个问题的办法是安装缺失的依赖包,执行命令`sudo apt-get install unity-lens-applications` 和 `sudo apt-get install unity-lens-files`
    2024-09-09
  • Apache Hudi结合Flink的亿级数据入湖实践解析

    Apache Hudi结合Flink的亿级数据入湖实践解析

    这篇文章主要为大家介绍了Apache Hudi结合Flink的亿级数据入湖实践解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-03-03
  • Nexus私服的搭建原理及教程解析

    Nexus私服的搭建原理及教程解析

    这篇文章主要介绍了Nexus私服的搭建原理及教程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Hadoop计数器的应用以及数据清洗

    Hadoop计数器的应用以及数据清洗

    今天小编就为大家分享一篇关于Hadoop计数器的应用以及数据清洗,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01

最新评论