节点JS HTTP2服务器TLS连接无法建立

时间:2019-04-28 19:17:23

标签: javascript node.js ssl http2

尝试在节点12上创建基本的HTTP2静态服务器。

这是我的Web服务器的类/功能:

import fs from 'fs';
import http2 from 'http2';

export default function(hostedPath, p, Debug){
    const {
        HTTP2_HEADER_METHOD,
        HTTP2_HEADER_PATH,
        HTTP2_HEADER_STATUS,
        HTTP2_HEADER_CONTENT_TYPE,
        HTTP_STATUS_INTERNAL_SERVER_ERROR,
        HTTP_STATUS_ACCEPTED
      } = http2.constants;

    let port = (typeof(p) !== "undefined")? p:8080;
    let secure = false;
    let key = null;
    let crt = null;

    let server;

    this.setPort = (p) => {
        port = p;
    }

    this.enableSSL = (c, k) => {
        // enable secure mode (SSL)
        this.secure = true;
        // read the key into the store var
        fs.readFile(k, (err, data) => {
            if(err){
                throw err;
            }
            key = data;
        })
        // read the key into the stor var
        fs.readFile(c, (err, data) => {
            if(err){
                throw err;
            }
            crt = data;
        })
    }

    let handleRequest = (stream, headers, flags) => {
        console.log(`Handling web request ${hadersJson} with flags ${flagsJson}`);
        if(Debug){
            try{
                // get requested file and read it
                //let ret = fs.readFileSync();
                let hadersJson = JSON.stringify(headers);
                let flagsJson = JSON.stringify(flags);
                let method = headers[HTTP2_HEADER_METHOD];
                let path = headers[HTTP2_HEADER_PATH];

                stream.respond({
                    [HTTP2_HEADER_STATUS]: 200,
                    [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain'
                });
                stream.on('error', (error) => console.error(error));
                stream.end('ok');

            }catch(e){
                console.error(`The web sever encounted error, ${e}`);
                stream.respond({
                    [HTTP2_HEADER_STATUS]: 500,
                    [HTTP2_HEADER_CONTENT_TYPE]: 'text/html'
                });
                stream.end(fs.readFileSync("Public/Errors/500.html"));
            }
        }
    }

    let WaitForRead = (callback) => {
        if (key !== null && crt !== null){
            console.log(`CRT and Key Loaded for Web Server`);
            callback();
        }else{
            setTimeout(() => {
                WaitForRead(callback);
            }, 500);
        }
    }

    let runServer = function(){
        server = (!secure)? http2.createServer():http2.createSecureServer({
            key: key,
            cert: crt
        });
        server.on("stream", handleRequest);
        server.listen(port);
        console.log(`Web Server running`);
    }

    let start = function(){
        WaitForRead(runServer);
    }

    let stop = () => {
        server.close();
    }

    this.start = ()=>{start()};
    this.stop = ()=>{stop()};
}

我从

开始
    webServer = new WebServer(`${BasePath}/public/`, 8090, Debug);
    webServer.enableSSL('localhost.crt', 'localhost.key');
    webServer.start();

键是the source

中示例的键

每当我尝试使用Chrome或Advanced REST Client连接时,都会收到ARC提示的错误消息

  

在建立安全TLS连接之前,客户端网络套接字已断开连接

0 个答案:

没有答案