node.js - express如何使用https?
伊谢尔伦
伊谢尔伦 2017-04-17 16:12:55
0
2
391

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

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
大家讲道理

Start normallyexpress项目在3000端口
然后用nginx服务器实现反向代理
再用nginx配置https

刘奇

Bare nodejs code:

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 originally had https module

--end--

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template