Rss Feed Tweeter button Facebook button Technorati button Reddit button Linkedin button Webonews button Delicious button Digg button Flickr button Stumbleupon button Newsvine button

A Waage Blog

Ruby, Rails, Life

Node.js and RESTify Server with both HTTP and HTTPS

with 4 comments

Here’s a quick guide on setting up a Node.js server using the restify module that accepts BOTH http and https connections.

We want to re-use as much code as possible because the two servers (HTTP and HTTPS) are identical besides the protocol.

In the example, we created a self-signed SSL certificate. If you need help on that, check out this quick guide on how to set up a self-signed SSL certificate on Ubuntu.

var restify = require('restify');
var fs = require('fs');

// Setup some https server options
var https_options = {
  key: fs.readFileSync('/etc/ssl/self-signed/server.key'),
  certificate: fs.readFileSync('/etc/ssl/self-signed/server.crt')

// Instantiate our two servers
var server = restify.createServer();
var https_server = restify.createServer(https_options);

// Put any routing, response, etc. logic here. This allows us to define these functions
// only once, and it will be re-used on both the HTTP and HTTPs servers
var setup_server = function(app) {
        function respond(req, res, next) {
           res.send('I see you ' +;

        // Routes
        app.get('/test/:name', respond);

// Now, setup both servers in one step

// Start our servers to listen on the appropriate ports
server.listen(80, function() {
  console.log('%s listening at %s',, server.url);

https_server.listen(443, function() {
  console.log('%s listening at %s',, https_server.url);

Written by Andrew Waage

February 27th, 2012 at 4:05 pm

Posted in Node.js

4 Responses to 'Node.js and RESTify Server with both HTTP and HTTPS'

Subscribe to comments with RSS or TrackBack to 'Node.js and RESTify Server with both HTTP and HTTPS'.

  1. [...] you can use the files server.key and server.crt in your web server setups. See my post on Setting up Node.js with to accept HTTP and HTTPS connections for a quick example of using this SSL certificate with [...]

  2. Thanks Andrew. You should put some ads somewhere on your site, I would have clicked to say thank you ;)

    Marc-Antoine Ross

    3 Oct 12 at 6:39 pm

  3. I did all this, and when I try to connect via curl I get an error:
    curl: (35) Unknown SSL protocol error

    (i have my self signed SSL certs created as you posted too)

    Pablo A

    16 Mar 13 at 8:48 am

  4. Hello. I tried in the way you explained. The code worked but the problem is when i go to browser and type: https://localhost/test/name, the browser shows me the https crossed with a red line. can you tell me how can i remove that?


    24 Aug 13 at 3:08 am

Leave a Reply