当前位置: 首页>NodeJS>ExpressJS>How can I configure expressjs to handle both http and https?

How can I configure expressjs to handle both http and https?

时间:2015-06-29 23:52 来源:网络整理 作者:KKWL 点击:
Following @ypocat 's comment you can enable https in your express.js application like so var http = require('http'); var https = require('https'); var express = require('express'); var fs = require('fs'); var app = express.createServer(); /

 

Following @ypocat 's comment you can enable https in your express.js application like so

var http = require('http'); var https = require('https'); var express = require('express'); var fs = require('fs'); var app = express.createServer(); // cutomize your app as ususal app.configure( function () { ... }); app.configure('production', function () { ... }); // .... // attach express handler function to TWO servers, one for http and one for https http.createServer(app.handle.bind(app)).listen(8080); https.createServer({ ca: fs.readFileSync('./server.ca-bundle'), key: fs.readFileSync('./server.key'), cert: fs.readFileSync('./server.crt') }, app.handle.bind(app)).listen(8081);

Note that you should receive server.ca-bundle, server.key and server.crt from a certificate authority.

Also as you will probably run node without sudo you need to make sure port 80(http) and 443(https) are open

# in Ubuntu sudo ufw status sudo ufw allow 80 sudo ufw allow 443

and to forward requests on 8080 to 80 and from 8081 to 443 respectively

# in Ubuntu iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080 iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8081

Hope this helps

 

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------