Node的https模块_创建HTTPS服务器

更新日期: 2018-05-04阅读: 2.9k标签: 模块

Node的https模块

  • HTTPS服务器使用HTTPS协议
  • 需要证书授权
  • SSL安全加密后传输
  • 使用443端口


创建HTTPS服务器

Microsoft Windows [版本 10.0.16299.125]
(c) 2017 Microsoft Corporation。保留所有权利。

C:\WINDOWS\system32>openssl genrsa -out privatekey.pem 1024
Generating RSA private key, 1024 bit long modulus
............++++++
.................................++++++
e is 65537 (0x10001)

C:\WINDOWS\system32>openssl req -new -key privatekey.pem -out certrequest.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:JS
Locality Name (eg, city) []:MJ
Organization Name (eg, company) [Internet Widgits Pty Ltd]:SV
Organizational Unit Name (eg, section) []:SW
Common Name (e.g. server FQDN or YOUR name) []:localhost
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:qweasdzxc
An optional company name []:ganmu

C:\WINDOWS\system32>openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
Signature ok
subject=/C=AU/ST=JS/L=MJ/O=SV/OU=SW/CN=localhost
Getting Private key

C:\WINDOWS\system32>openssl pkcs12 -export -in certificate.pem -inkey privatekey.pem -out certificate.pfx
Enter Export Password:
Verifying - Enter Export Password:

C:\WINDOWS\system32>


https模块

var https = require('https');
var fs = require('fs');
var pk = fs.readFileSync("./certs/privatekey.pem");
var pc = fs.readFileSync("./certs/certificate.pem");

var opts = {
    key:pk,
    cert:pc
};

var server = https.createServer(opts, function(req, res) {
    console.log(req.url);
    if(req.url !== './favicon.ico') {
        res.setHeader("contentType","text/plain");
        res.write("hello");
        res.end();
    }
});

server.listen(443, "localhost", function() {
    console.log('https server is listening on 443');
});

浏览器会拦截访问,提示该访问为不安全链接,因为证书无效。


HTTPS客户端

var https = require('https');
var opts = {
    hostname:"npmjs.org",
    port:443,
    path:"/",
    method:"GET",
    agent:false
};

opts.agent = new https.Agent(opts);


var req = https.request(opts, function(res) {
    console.log(res.statusCode);
    console.log(JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function(chunk) {
        console.log(chunk);
    });
});

req.on("error", function(err) {
    console.log(err);
})

req.on("socket", function(socket) {
    socket.setTimeout(10*1000);
    socket.on("timeout", function() {
        req.abort();
    });
})

req.end();

运行

F:\后台开发\node-server-test>node client.js
301
{
    "server":"nginx/1.12.2",
    "date":"Thu, 26 Apr 2018 16:36:16 GMT",
    "content-type":"text/html",
    "content-length":"185",
    "connection":"close",
    "location":"https://www.npmjs.com/"
}
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>

链接: https://www.fly63.com/article/detial/732

ES6模块功能:export和import的加载方式

ES6之前已经出现了js模块加载的方案,最主要的是CommonJS和AMD规范。commonjs主要应用于服务器,实现同步加载,如nodejs。AMD规范应用于浏览器,如requirejs,为异步加载。

如何让 node 运行 es6 模块文件,及其原理

最新版的 node 支持最新版 ECMAScript 几乎所有特性,但有一个特性却一直到现在都还没有支持,那就是从 ES2015 开始定义的模块化机制。而现在我们很多项目都是用 es6 的模块化规范来写代码的,包括 node 项目

module、export、require、import的使用

module每个文件就是一个模块。文件内定义的变量、函数等等都是在自己的作用域内,都是自身所私有的,对其它文件不可见。在module中有一个属性exports,即:module.exports。它是该模块对外的输出值,是一个对象。

Node.js - 模块系统

模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的。换言之,一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码、JSON 或者编译过的C/C++ 扩展。Node.js 提供了 exports 和 require 两个对象

ES模块基础用法及常见使用问题

ES6中引入了模块(Modules)的概念,相信大家都已经挺熟悉的了,在日常的工作中应该也都有使用。本文会简单介绍一下ES模块的优点、基本用法以及常见问题。

ES6 export 和 export default的区别

ES6中 export 和 export default 与 import使用的区别,使用 react native 代码详解,现在流行的前端框架,angular+ 主要使用 export 导出模块,react native 中使用 export default 导出模块,如今编辑器非常强大,安装插件会自动弹出模块名称,知道其导出怎么使用就可以了

export和export default的区别

export与export default均可用于导出常量、函数、文件、模块;你可以在其它文件或模块中通过import+(常量 | 函数 | 文件 | 模块)名的方式,将其导入,以便能够对其进行使用;

关于export和export default你不知道的事

网上有很多关于export和export default的文章,他们大部门都是只讲了用法,但是没有提到性能,打包等关键的东西。大家应该应该能理解import * from xxx会把文件中export default的内容都打包到文件中,而import {func} from xxx只会把文件中的func导入

最全的前端模块化方案

模块化主要是用来抽离公共代码,隔离作用域,避免变量冲突等。将一个复杂的系统分解为多个模块以方便编码。会讲述以下内容:CommonJS、AMD 及 核心原理实现、CMD 及 核心原理实现

Vue 中如何正确引入第三方模块

配置 webpack ProvidePlugin 全局引入,假设要使用到 jquery,那么可以通过配置 webpack 的 ProvidePlugin 的插件来全局引入;另外一种比较靠谱的方法是将第三方模块打包成插件,如我需要全局使用 echarts,那么在 src 目录下新建一个 lib

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!