suchen

Heim  >  Fragen und Antworten  >  Hauptteil

node.js - express如何使用https?

在腾讯云申请的免费版DVSSL证书,怎么在express项目中使用啊?

伊谢尔伦伊谢尔伦2786 Tage vor395

Antworte allen(2)Ich werde antworten

  • 大家讲道理

    大家讲道理2017-04-17 16:14:55

    正常启动express项目在3000端口
    然后用nginx服务器实现反向代理
    再用nginx配置https

    Antwort
    0
  • 怪我咯

    怪我咯2017-04-17 16:14:55

    裸nodejs代码:

    const crypto = require('crypto'),
      fs = require("fs"),
      http = require("http");
    
    var privateKey = fs.readFileSync('privatekey.pem').toString();
    var certificate = fs.readFileSync('certificate.pem').toString();
    
    var credentials = crypto.createCredentials({key: privateKey, cert: certificate});
    
    var handler = function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
    };
    
    var server = http.createServer();
    server.setSecure(credentials);
    server.addListener("request", handler);
    server.listen(8000);
    
    

    express:

    var express = require('express');
    var https = require('https');
    var http = require('http');
    var app = express();
    
    http.createServer(app).listen(80);
    https.createServer(options, app).listen(443);
    

    PS: nodejs6本来有https模块啊

    --end--

    Antwort
    0
  • StornierenAntwort