Secure express.js 3 申請

使用 express.js 進行安全連線的配置(從版本 3 開始):

var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey  = fs.readFileSync('sslcert/server.key', 'utf8');
var certificate = fs.readFileSync('sslcert/server.crt', 'utf8');

// Define your key and cert

var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();

// your express configuration here

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

// Using port 8080 for http and 8443 for https 

httpServer.listen(8080);
httpsServer.listen(8443);

通過這種方式,你可以向本機 http / https 伺服器提供快速中介軟體

如果你希望應用程式在低於 1024 的埠上執行,則需要使用 sudo 命令(不推薦)或使用反向代理(例如 nginx,haproxy)。