美文网首页
创建Https服务器

创建Https服务器

作者: manmouse | 来源:发表于2018-01-15 14:44 被阅读0次

1. 利用openssl生成证书文件:

#生成私钥key文件

openssl genrsa1024> /path/to/private.pem

#通过私钥文件生成CSR证书签名

openssl req -new-key /path/to/private.pem -out csr.pem

#通过私钥文件和CSR证书签名生成证书文件

openssl x509 -req -days365-in csr.pem -signkey /path/to/private.pem -out /path/to/file.crt

2. nodejs+express创建服务器

var app = require('express')();

var fs = require('fs');

var http = require('http');

var https = require('https');

var privateKey  = fs.readFileSync('./private.pem', 'utf8');

var certificate = fs.readFileSync('./file.crt', 'utf8');

var credentials = {key: privateKey, cert: certificate};

var httpServer = http.createServer(app);

var httpsServer = https.createServer(credentials, app);

var PORT = 8888;

var SSLPORT = 9999;

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!');

    }

});

相关文章

网友评论

      本文标题:创建Https服务器

      本文链接:https://www.haomeiwen.com/subject/istdoxtx.html