SSL with node.js, express and socket.io

Using HTTPS with express is almost as simple as replacing require(‘http’) with require(‘https’). For my application I am using a self-signed certificate that I created after following the directions on Nate Good’s blog. I placed the certs in a folder called cert.

I initialized the variables in preparation for two servers following this Stackoverflow question. One server for https listening on 443 and another for http listening on port 80. Then I wrote the http server to forward all requests to the https server. So that node doesn’t run as root I edited the iptables to map port 80 to 8081 and port 443 to 8080.

The following code forwards all requests for HTTP to HTTPS.

 

Then on the client side I had to setup socket.io to use the secured port as well.

 

The process was first to obtain or create an SSL Certificate. Then create an express server that requires ‘HTTPS’. Finally, setup the client JavaScript to reply on the HTTPS secure port.
EDIT 7/24/13: Also check out the documentation for HTTPS http://nodejs.org/api/https.html.

 

转自:http://chrislarson.me/blog/ssl-nodejs-express-and-socketio

关于SSL证书生成,请参考:http://blog.tangjianwei.com/2009/01/13/how-to-create-ssl-certificate-for-your-own-website/

https://konklone.com/post/switch-to-https-now-for-free

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">