Given an SSL key and certificate, how does one create an HTTPS service?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Keeping track of variable instances
Found this question while googling "node https" but the example in the accepted answer is very old - taken from the docs of the current (v0.10) version of node, it should look like this:
The minimal setup for an HTTPS server in Node.js would be something like this :
If you also want to support http requests, you need to make just this small modification :
I found following example.
https://web.archive.org/web/20120203022122/http://www.silassewell.com/blog/2010/06/03/node-js-https-ssl-server-example/
This works for node v0.1.94 - v0.3.1.
server.setSecure()
is removed in newer versions of node.Directly from that source:
The above answers are good but with Express and node this will work fine.
Since express create the app for you, I'll skip that here.
Update
Use Let's Encrypt via Greenlock.js
Original Post
I noticed that none of these answers show that adding a Intermediate Root CA to the chain, here are some zero-config examples to play with to see that:
Snippet:
This is one of those things that's often easier if you don't try to do it directly through connect or express, but let the native
https
module handle it and then use that to serve you connect / express app.Also, if you use
server.on('request', app)
instead of passing the app when creating the server, it gives you the opportunity to pass theserver
instance to some initializer function that creates the connect / express app (if you want to do websockets over ssl on the same server, for example).