cari

Rumah  >  Soal Jawab  >  teks badan

node.js - express如何使用https?

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

伊谢尔伦伊谢尔伦2786 hari yang lalu390

membalas semua(2)saya akan balas

  • 大家讲道理

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

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

    balas
    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--

    balas
    0
  • Batalbalas